One of the important use of the .htaccess file is to redirect users to a custom error page depending on the specific web server error they encounter. Your website visitors need know why the page which he/she is visiting does not exist any more, what happens to the page that he is visiting. Search Engine like google need know why the pages are missing too. For good visitor experience and good SEO results, you need customer error pages.
Creating custom error pages is very useful, it allows you to show web site visitors and search engines a friendly error message, for instance if a URL on your web site does not work. This avoids the unfriendly ‘404 File Not Found’ error and allows you to display a friendly error, explaining possible solutions and guiding the visitor back into your web site content, rather than leaving them frustrated and lost.
Set up a simple 404 error page
To set-up custom error documents, create a .htaccess file following the main instructions and guidance which includes the following text:
ErrorDocument 404 http://yourdomainname.com/yourdirectory/404error.html
The above line tells the Apache Web Server to display the document located at http://yourdomainname.com/yourdirectory/404error.html ( the 404error.html file can be under your domain name or another domain name ) whenever a 404 (file not found) error occurs.
you could even use pure HTML in the .htaccess file to be displayed in case of an error, as below. Note that if you want to use HTML, you must start the HTML with a quotation mark, however you should not put one at the other end of the HTML (you can include quotation marks within the HTML itself as normal).
ErrorDocument 404 "Ooops, that page was not found. Please try a different one or email the site owner for assistance.
Setup further error documents
A server reponse code is a three digit number sent by a server to a user in response to a request for a web page or document. They tell the user whether the request can be completed, or if the server needs more information, or if the server cannot complete the request. Usually, these codes are sent ‘silently’ – so you never see them, as a user – however, there are some common ones that you may wish to set up error pages for, and they are listed below. Most people will only ever need to set up error pages for server codes 400, 401, 403, 404 and 500.
‘401 Unauthorised’, ‘403 Forbidden’, and ‘500 Internal Server’ error messages, create a .htaccess file following the main instructions and guidance which includes the following text:
ErrorDocument 400 /error_pages/400.html
ErrorDocument 401 /error_pages/401.html
ErrorDocument 403 /error_pages/403.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 500 /error_pages/500.html
It’s all very well displaying a friendly error message, but more importantly you need to resolve the error.
HTTP response status codes
- 300 – Multiple Choices
- 301 – Moved Permanently
- 302 – Found
- 303 – See Other
- 304 – Not Modified
- 305 – Use Proxy
- 307 – Temporary Redirect
- 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
- 500 – Internal Server Error
- 501 – Not Implemented
- 502 – Bad Gateway
- 503 – Service Unavailable
- 504 – Gateway Timeout
- 505 – HTTP Version Not Supported