Creating custom error pages

From DreamHost
Jump to: navigation, search

Why would I want to make custom error pages?

Ever have one of those days where your spelling skills randomly fall off the face of the planet and you enter abott.html when you meant to type about.html and BLATT! your face is an ugly, boring, and intimidating page with standard black-on-white text that tells you the page was not found? Yeah, your visitors have those kinds of days all the time, too (check out your error logs sometime!).

Wouldn't it be great if all that hard work you poured into making your website layout "just so" could apply to error messages as well and (even better) actually give the user some helpful hints on how they might find the thing they were looking for in the first place?

Well there's great news… you can!

The easy way

Luckily, the fine folks at DreamHost foresaw that this would be an issue many people would want to address, so they've built in some "magic" file names that, if present in your domain's root directory, will automatically be loaded when a user obtains a certain error. These are:

Error What it means File name
Error 401 Failed Authorization failed_auth.html
Error 403 Forbidden forbidden.html
Error 404 File Not Found missing.html
Error 500 Internal Server Error internal_error.html

If these documents are not available, a second error message will also be stated letting the person know those pages could not be found. The information listed below remedies both the designated output for the errors and resolves the second error you would receive for not having these files.

Turning it off

If you prefer to have Apache display its regular error messages, you can undo DreamHost's "magic" by putting the following in your .htaccess:

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default

The .htaccess way

Why would you want to go mucking around with .htaccess when DreamHost gives you a nice easy way out?

  • You want to be able to cover more errors than just those 4 (such as 503 Service Unavailable)
  • You want your files organized more clearly (for example, you want all your error messages in a folder called /errors)
  • You want to use a scripting language such as PHP with your errors so you can do more advanced things such as logging, etc.
  • You're just a geek and like to tinker around with things

Whatever your reasons, here's how you do it. Place an .htaccess file in your root directory and add any ErrorDocument want. Notice below that some of the errors are being handled by php scripts in the /cgi-bin/ directory, including using the $_GET['code'] parameter to pass the error. The status code will also be available in your servers environment using the REDIRECT_STATUS variable, which can be helpful from within your /cgi-bin/error.php

ErrorDocument 400 /error-deal.php
ErrorDocument 402 /error-deal.php
ErrorDocument 403 /error/403/index.html
ErrorDocument 404 /cgi-bin/error.php?code=404
ErrorDocument 405 /error/405/index.php
ErrorDocument 406 /cgi-bin/error.php?code=406
ErrorDocument 407 /cgi-bin/error.php?code=407
ErrorDocument 408 /cgi-bin/error.php?code=408
ErrorDocument 409 /cgi-bin/error.php?code=409
ErrorDocument 410 /cgi-bin/error.php?code=410
ErrorDocument 411 /cgi-bin/error.php?code=411
ErrorDocument 412 /cgi-bin/error.php?code=412
ErrorDocument 413 /cgi-bin/error.php?code=413
ErrorDocument 414 /cgi-bin/error.php?code=414
ErrorDocument 415 /cgi-bin/error.php?code=415
ErrorDocument 416 /cgi-bin/error.php?code=416
ErrorDocument 417 /cgi-bin/error.php?code=417
ErrorDocument 418 /cgi-bin/error.php?code=418
ErrorDocument 419 /cgi-bin/error.php?code=419
ErrorDocument 420 /cgi-bin/error.php?code=420
ErrorDocument 421 /cgi-bin/error.php?code=421
ErrorDocument 422 /cgi-bin/error.php?code=422
ErrorDocument 423 /cgi-bin/error.php?code=423
ErrorDocument 424 /cgi-bin/error.php?code=424
ErrorDocument 425 /cgi-bin/error.php?code=425
ErrorDocument 426 /cgi-bin/error.php?code=426
ErrorDocument 500 /error/500/index.html
ErrorDocument 501 /cgi-bin/error.php?code=501
ErrorDocument 502 /cgi-bin/error.php?code=502
ErrorDocument 503 /error-503.php
ErrorDocument 504 /cgi-bin/error.php?code=504
ErrorDocument 505 /cgi-bin/error.php?code=505
ErrorDocument 506 /cgi-bin/error.php?code=506
ErrorDocument 507 /cgi-bin/error.php?code=507
ErrorDocument 508 /cgi-bin/error.php?code=508
ErrorDocument 509 /cgi-bin/error.php?code=509
ErrorDocument 510 /cgi-bin/error.php?code=510

Available ErrorDocument Codes

After going through the Apache HTTP Server code I was able to find out a total of 57 ErrorDocuments are supported on Apache. Here are some of them.

  • 100 Continue
  • 101 Switching Protocols
  • 102 Processing
  • 200 OK
  • 201 Created
  • 202 Accepted
  • 203 Non-Authoritative Information
  • 204 No Content
  • 205 Reset Content
  • 206 Partial Content
  • 207 Multi-Status
  • 300 Multiple Choices
  • 301 Moved Permanently
  • 302 Found
  • 303 See Other
  • 304 Not Modified
  • 305 Use Proxy
  • 307 Temporary Redirect
  • 400 Bad Request
  • 401 Authorization Required
  • 402 Payment Required
  • 403 Forbidden
  • 404 Not Found
  • 405 Method Not Allowed
  • 406 Not Acceptable
  • 407 Proxy Authentication Required
  • 408 Request Time-out
  • 409 Conflict
  • 410 Gone
  • 411 Length Required
  • 412 Precondition Failed
  • 413 Request Entity Too Large
  • 414 Request-URI Too Large
  • 415 Unsupported Media Type
  • 416 Requested Range Not Satisfiable
  • 417 Expectation Failed
  • 422 Unprocessable Entity
  • 423 Locked
  • 424 Failed Dependency
  • 425 No code
  • 426 Upgrade Required
  • 500 Internal Server Error
  • 501 Method Not Implemented
  • 502 Bad Gateway
  • 503 Service Temporarily Unavailable
  • 504 Gateway Time-out
  • 505 HTTP Version Not Supported
  • 506 Variant Also Negotiates
  • 507 Insufficient Storage
  • 510 Not Extended

mod_rewrite trick

This snippet internally rewrites (the browser url won't change) all requested uri's that are not a file or a directory and displays the /404.php page in place of them. This is what many blogs and CMS software use.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /404.php [L]

Related Articles

External Links