Creating Custom Error Messages in Apache
The .htaccess file is a file found in Apache servers that allows you to manipulate the behaviour of the server. A very common use is to create custom error messages for server errors. Examples of such errors are “404 Not Found” (say when you click on a broken link), and the “500 Internal Server Error” (a script failed). Most hosts allow you to have your own .htaccess file, and so now we will look at only one of the things we can do with a custom .htaccess.
Some quick notes before we start: under Windows, you cannot save a file with the name of “.htaccess”. An easy trick is to create a file called htaccess.txt and edit that. Uploading of the file (regardless of its name) must be done in TEXT/ASCII mode (not BINARY). Finally, under Unix, any file that starts with a ‘.’ is invisible, so when you upload your file, you may not see it. If you uploaded a file called htaccess.txt, just rename it to .htaccess using your FTP client.
Custom Error Messages
What you can do with .htaccess is tell the server to display a special page to the user in case of an error. This page should, ideally, tell the user that something is wrong, down-play the fact that someone messed up (probably you as the webmaster!), and also provide a set of links to the major sections of the site, so the user can at least look for what is missing. Another nice use is to include a search form on the 404 page. You can also tell the server to run a CGI script, instead of simply displaying a static page. If you do this, you can tell the script to log the error for you, or the script can send you an email about the error.
So how do we implement this? Since the .htaccess file is a simple text file, you simply add a line of text to the file, one for each error you want to trap. The general format is:
ErrorDocument 3DigitErrorCode /path/to/your/error/file/or/script
The 3-Digits error codes are as follows:
|Error 400||Bad Request|
|Error 401||Authorization Required|
|Error 500||Server Configuration Error|
Thus the following line tells the server to run the error404.cgi script if the server cannot find the requested page.
|ErrorDocument 404 /cgi-bin/error404.cgi|
This way, you can trap as many errors as you want. Below is an example of a .htaccess file, just copy and paste into your file, but change the paths:
|ErrorDocument 404 /path/to/your/error/file/or/script|
ErrorDocument 500 /path/to/your/error/file/or/script
ErrorDocument 403 /path/to/your/error/file/or/script
A very important point to note is that the paths should be the relative paths. This means that the paths would be what you would type on the machine to get to the files, not what you would type in the address box in a browser.
Full path would be
Absolute path would be /home/yoursite.com/path/to/error/page
Relative path would be /path/to/error/page
To summarise the steps, they are as follows:
- Create a text file, and add to it a line for each error.
- Upload the file to the server, and rename it .htaccess.
- Upload the files/script that the .htaccess file calls in each error.
- TEST! By typing a weird URL in the address box – you should get the 404 custom error.
Now you are done!
FrontPage Warning: FrontPage sites have a .htaccess file in the root directory that is created when the FrontPage extensions are installed. FrontPage users should proceed with caution and make a backup copy of their .htaccess file before making any changes. Incorrect changes to your .htaccess file can result in your site being unreachable.