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 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.
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 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:
* 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
Keep posting stuff like this i really like it