Home > Tutorials > Know Your Server > Custom Errors (Apache)

Custom Error Pages with Apache

The Apache server is probably the most widely used for hosting web sites, and is fairly easy to configure various custom error messages through the use of a .htaccess file.

The .htaccess file

This file, if available for use on the server, can do a whole manner of things. One of the most useful applications it has is for allowing a site owner to customise their error messages, not only for each different type of error, but also for each different directory if they wish. Other uses for the .htaccess file include invisibly using it to redirect users to another page without the use of a meta refresh, or it can even restrict access to areas of the site (which is useful for any kind of members section). Right now however we are going to concentrate on using it to customise error pages.

Creating a .htaccess file

There is no special program that you need to be able to edit or create a .htaccess file, since all it is, is a normal txt file (the . at the start of the name just indicates that it is a hidden file). Create a new text file in the usual way and edit it in a text editor (a simple editor is recommend). If you are using a Windows system for developing your web site you will soon realise that Windows does not allow you to rename a file .htaccess. One way to get around this is to name it something obvious such as htaccess.txt and then rename it once it is uploaded to the server.

The entries you will need to have the server open your error page are straightforward enough, and follow the format outlined just below.

ErrorDocument "3 digit error code" "text message or URL"

So, a typical .htaccess file used for custom errors might look something like this.

ErrorDocument 403 http://www.mysite.com/forbidden.html
ErrorDocument 404 http://www.mysite.com/cgi-bin/notfound.pl
ErrorDocument 500 "The server got very confused and couldn't help you!"

If you were visiting a site that was using this in it's .htaccess file then you would be taken to a page with a prewritten message on it if you weren't authorised to view the page, or sent to a script if it couldn't find the page, or given a text message if there was a server error. Notice how you are not limited to use files and scripts on your own server (although it is recommended that you do!). If you wish to have the server reply with just a simple text message, just put the message in double quotes. You should also be careful to make sure that each of your .htaccess file entries has its own line.

When you come to upload your .htaccess files, you should be aware that it makes a difference as to where you put them. A single .htaccess file in the root directory will have an effect on the entire site, but it can be over-ridden by putting a different .htaccess file in your sub directories. The server will look for a .htaccess file in the same directory as the error first, if it can't find one it will move one directory back until it finds one. Of course, if it doesn't find one then it will just use the servers or browsers default error page.


Noticed a problem or got a question or comment?


This site will look much better in a browser that supports web standards, but it is still accessible to any browser or Internet device.


About this Page

Author: Rosemarie Wise
Originally Published: Fri 15th Dec, 2000
Last Revised: Sun 4th Nov, 2001
URL: http://websiteowner.info/tutorials/server/apacheerrors.asp

Want to use this tutorial on your own site? Learn more...


The Web Site Owner's Resource © 2000-2007 Rosemarie Wise