Creating custom error pages
This article describes how to create custom 404 error pages. It will also guide you in the creation of pages that replace any of the standard HTTP errors.
Have you ever seen customized error pages that have an actual html file that explains what could have gone wrong instead of a boring, server-generated black and white page?
The answer lies in a small feature called .htaccess. With it, you can create customized error pages, amongst other things.
Customizing error messages for your site:
- Create the HTML pages you want to use as your error message. Give them an easy to remember name like 404.html, and 403.html
- Upload to any place in your 'web root'. (where your HTML files are located)
- Create a text file named .htaccess at the same level that your main index.html page is located (the web root). Add the lines which specify the substitution.
EXAMPLES:
(note you can place error pages where ever you wish and use relative or absolute addressing):
Example .htaccess file:
ErrorDocument 401 /nopasswd.html
ErrorDocument 403 /error_pages/forbidden.html
ErrorDocument 404 /not_found.html
Now try going to a bogus page on your site, for example foofake.html and you should now see what you entered in not_found.html. Let us explain what just happened. When you accessed foofake.html the server found that it was not there, it then proceeded to check your .htaccess file for help. In your .htaccess file it found the line
ErrorDocument 404 /not_found.html
..and interpreted it. The result was that because a 404 server error is a Not Found error, it redirected you to not_found.html.
Let's look at the syntax of Error Document and .htaccess:ErrorDocument 404 /not_found.html
ErrorDocument is the first thing you see. It is the basis for error redirection so always leave it the same. 404 is the server error number. See the list of server error numbers and their descriptions below. The not_found.html is where the error document file to be shown is found. The not_found.html means it would be located at http://www.mt-example.com/not_found.html. This can be changed to whatever you wish.
Error Chart
Error in Client
- 400 Bad syntax
- 401 Unauthorized
- 402 Not Used (Payment Granted)
- 403 Forbidden
- 404 Not Found
Error in Server
- 500 Internal Error
- 501 Not Implemented
- 502 Overloaded
- 503 Gateway Timeout
You can also put your .htaccess file in a sub directory of your site and still have it work, but only for that subdirectory. For example if you put it in /foofolder/ and there was an error in /foofolder/, it would be checked for what error page to show before checking the .htaccess in your web root.
Revisions:
12-18-2008: Added Revisions Box
Fields marked with an asterisk(*) are required. Comment on this article