Creating Custom Error Messages in Apache

Introduction

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 sever 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).

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 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 403: Forbidden
  • Error 404: Missing
  • 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 full absolute paths, not the virtual paths. These 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.

To summarise the steps, they are as follows:

  1. Create a text file, and add to it a line for each error.
  2. Upload the file to the server, and rename it .htaccess.
  3. Upload the files/script that the .htaccess file calls in each error.
  4. TEST! By typing a weird URL in the address box - you should get the 404 custom error.

Now you are done!

As a final note, a good script to use for this task is the one at BigNoseBird. It is called Birdtrap.

Leave a Reply

You must be logged in to post a comment.

 

Site Navigation

Popular Pages

The most popular pages on eKstreme.com.

Search

Subscribe

Subscribe to RSS 2.0 feed

Community

 
thermodelly