KB / Web Programming / Error messages
From DreamHost
Contents |
Parent Article
Custom 404 (File Not Found) error pages.
I've noticed that some people have created a customized web page that appears when you try to access a page on their site that does not exist. How can I do that?
404: File Not Found
When a visitor to your site attempts to load a page that does not exist, they receive a generic error message stating that the requested page cannot be found. This is what is known as a '404 error', and is often discovered through broken links to to pages on your site.
The Solution
Of course, the first thing you want to do is make sure that all links within your site are accurate. Your visitors will thank you for it.
However, that won't catch incorrect links from other sites or the occasional mistyping of a URL. To catch these kinds of errors, you need to create a custom 404 error page, using the same methods you would to create any other page. Then, name it 'missing.html' and upload it to the root level of your domain. From that point on, it will replace the generic '404: File Not found' message that your visitors will encounter.
Tips
First off, your custom 404 error page should state something to the effect of why the expected page did not appear. This will clue your visitors in to the fact that they should probably back up and try again, or report a broken link if they got to the error from a link on your site.
Internet Explorer doesn't (by default) display custom error pages, preferring to display its own error message. If you would like to override this, you can put hidden text in the page to increase the filesize to just over 512 bytes; IE will then display your custom error page.
You may also want to provide some links to the main sections of your web site, to help them move on to the area they want to view. You can consider this to be a helpful guide that appears when you look lost - a site map or directory would probably be quite welcome by that point. If you are feeling especially helpful or ambitious, you might want to even consider adding a search engine to this page to help your audience find its way.
Last but not least, you should provide absolute links, such as:
A HREF="http://mydomain.com/info/"
...instead of relative links, such as:
A HREF="../info/"
...so that these links don't become broken at any location of your site. Remember, since 404 errors can occur anywhere on your site, you can't count on them only happening within a given directory structure.
Custom 401 (Failed Authorization) error pages.
Please follow the instructions for making a Custom 404 error page but instead of naming the custom file missing.html, name the custom file failed_auth.html (and put it in your web directory.. not the root of your home directory).
This page will show up when somebody doesn't successfully enter their password to get into a password-protected area of your web site.
Custom 403 (Forbidden) error pages.
Please follow the instructions for making a Custom 404 error page but instead of naming the custom file missing.html, name the custom file forbidden.html.
This page will show up when a file isn't set to be world readable, or when you've specifically blocked a user (probably by their ip or hostname) from accessing that page via an .htaccess file, OR when a user just cancels their attempt to log in.
Custom 500 (Internal Server Error) error pages.
Please follow the instructions for making a Custom 404 error page but instead of naming the custom file missing.html, name the custom file internal_error.html.
This page will show up when a script on your web site has an internal error, instead of the default page.
Alternate Method for Custom Error Pages (allows for scripting languages).
Submitted by Regan Johnson from http://www.soaptray.com
For those of you who wish to use PHP, CGI, or Server Side Includes as your language of an error page, you may do so by using Apaches .htaccess file. Using PHP or CGI as an error page allows you to display more information about the error, and use dynamic elements as part of the error. For instance, you can display the address which they typed in that was wrong, current date and time, or even set it up to redirect the user to your start page.
Step 1: Creating the .htaccess file
First, you will want to create the .htaccess file, and upload it to your domains root. If you already have an existing .htaccess file, you may skip this step. In windows, you are not allowed to rename a file to .htaccess. To overcome this, you must upload a file (such as htaccess.txt) and rename it once on the server.
Step 2: Editing .htaccess file
Once we have the file in place, you will want to add the following information. Each on their own line.
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 401 /401.php
ErrorDocument 500 /500.php
Save the .htaccess file.
If you need to, rename the reference to the file names in the .htaccess file to whatever your error files are going to be. Instead of 404.php, you may want to use not_found.shtml. This is completely up to you.
Step 3: Creating and Uploading the error files
Now that we have the references set up, go ahead and create the error pages. In my example above, you would create a file named 404.php and upload it to the root of my domain. Do so for the other custom pages you wish to include (403.php, 401.php, and 500.php).
Step 4: Test and Enjoy
In order to test out the 404 error page, head over to your web page URL http://www.mydomain.com/asdsadlksahj which should trigger the 404.php page you created to be served.

