Setting Up An htaccess Error Document or Error Page

1

Posted on : 23-Jan-2010 | By : jontroth | In : .htaccess file, Tricks & Treats

I am sure you have clicked on a link only to end up with a generic 404 page not found error, most of the time caused because the page was removed from the site though it was still indexed in the search engines.

A great feature with the .htaccess file is being able to specify your own error documents, pointing an error to a custom page. Before you open up your .htaccess file in notepad you should get familiar with the servers error codes and what pages you should and shouldn’t use.

If your a novice to the .htaccess file the make sure you have read “The Great World of the .htaccess File” before continuing this article.

The most common codes that you will probably make an error document for are 404 and 500.

404 – Page Not Found
This is when the requested file can not be found, or doesn’t exist.

500 – Internal server error
This is a  generic error message, given when no more specific message is suitable.

Other errors that you might consider customizing are the 400, 401 and 403 errors.

400 – Bad Request
This is usually caused by a bad syntax in the url or within a script on the server.

401 – Authorization Required
This is used when a user tries and fails to enter a protected area of your site without the proper credentials.

403 – Forbidden
This is when a file with strict permissions is not allowing a limited permissions user to access the requested page.

Be careful writing a redirect command for other server codes as you could cause an infinite loop. A good example would be any of the 200 codes.

Writing the ErrorDocument command is straight forward and easy to understand. First you start the command with “ErrorDocument” and then a space, after that enter the error code that you want to use, another space and then you can enter either a root path, absolute path, or inline message.

URLs can begin with a forward slash (/) for local URLs, or you can use a full URL:

ErrorDocument 500 http://foo.domain.com/internal_error.html
ErrorDocument 404 /errors/notfound.html

Instead of directing the error to a page you can output an inline message or html.
Text in this context must begin with a single double-quote character ["], but you must not close with a double-quote character.

ErrorDocument 404 “<body bgcolor=#ffffff><h1>Page not found</h1></body>

I personally stay away from inline messages and utilize custom pages.

You can store your error pages anywhere in you sites directory, though I recommend creating a folder labelled “error_document” in the root of the directory, and name your files within that folder accordingly. For example a 404 page would be labelled “404_not_found.html”. This file structure will allow you to write the error redirect from the root of the directory () and also allow you the option of controlling if the folder is or isn’t indexed by search engine spiders.

If you use an error document handler for each of the error codes mentioned above, the htaccess file would look like the following with each redirect command is on its own line:

ErrorDocument 400 /error_document/badrequest.html
ErrorDocument 401 /error_document/authrequired.html
ErrorDocument 403 /error_document/forbidden.html
ErrorDocument 404 /error_document/notfound.html
ErrorDocument 500 /error_document/servererror.html

You should be familiar with all the servers codes.
The following are a list of Apache server codes:

Informational
* 100 – Continue
* 101 – Switching Protocols

Successful
* 200 – OK
* 201 – Created
* 202 – Accepted
* 203 – Non-Authoritative Information
* 204 – No Content
* 205 – Reset Content
* 206 – Partial Content

Redirection
* 300 – Multiple Choices
* 301 – Moved Permanently
* 302 – Found
* 303 – See Other
* 304 – Not Modified
* 305 – Use Proxy
* 307 – Temporary Redirect

Client Error
* 400 – Bad Request
* 401 – Unauthorized
* 402 – Payment Required
* 403 – Forbidden
* 404 – Not Found
* 405 – Method Not Allowed
* 406 – Not Acceptable
* 407 – Proxy Authentication Required
* 408 – Request Timeout
* 409 – Conflict
* 410 – Gone
* 411 – Length Required
* 412 – Precondition Failed
* 413 – Request Entity Too Large
* 414 – Request-URI Too Long
* 415 – Unsupported Media Type
* 416 – Requested Range Not Satisfiable
* 417 – Expectation Failed

Server Error
* 500 – Internal Server Error
* 501 – Not Implemented
* 502 – Bad Gateway
* 503 – Service Unavailable
* 504 – Gateway Timeout
* 505 – HTTP Version Not Supported

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Comments (1)

Keep posting stuff like this i really like it

Write a comment

Rss Feed Tweeter button Facebook button Technorati button Reddit button Myspace button Linkedin button Webonews button Delicious button Digg button Flickr button Stumbleupon button Newsvine button Youtube button