by Tom Dahm,
Chief Operations Officer,
Do you rent space on someone else's Web server? Even if you do, you can still customize much of the server's behavior. It's easier than you might think. This tip will show you how to change your Web server to use a custom error page. You can serve a page that has your Website's look and feel, instead of the generic "404 - File Not Found" page.
Web servers are highly configurable applications whose behavior is controlled by a series of commands called server directives. Apache, the world's most popular Web server, supports about 200 of these directives.
Server directives are stored in a series of server configuration files. These files are controlled by your server administrator, and only he or she can change them. That's a good idea, since most directives are global; changing them changes the way the server works for everyone.
But some server directives control things that are local to each virtual host. You need a way to set these directives so that they apply only to your Web site and not to all sites running on the server.
Fortunately most Web servers let you do this through something called an ".htaccess" file (Notice the dot at the beginning of the file name - it's required). The .htaccess file is a text file that's placed in the domain root for your site; in other words, you put it in the same directory as your site's home page. You may have to create a fresh .htaccess file if there's not already one there.
These .htaccess files are most often used to put files and directories under password protection, but they can be used for a lot of other things too, like server-side redirects. Apache allows over 40 of its 200 directives to be placed in an .htaccess file. To set up a custom error page, we only need to use one of these directives, called ErrorDocument. To use this directive, add this line to your .htaccess file:
ErrorDocument 404 /error_page.html
This line has of three parts:
- ErrorDocument is the server directive itself.
- A server status code to which this directive applies.
- The HTML file that should be served when the status code occurs.
In this example the status code is 404, which is the standard "file not found" error code you get when clicking on a broken link.
You can use different ErrorDocument directives to apply to different errors by adding extra ErrorDocument lines to the .htaccess file. So you might add a second line that looks like this:
ErrorDocument 500 /script_error.html
This would make the server use a different page when a CGI script has an internal error. The table below shows some common server status codes and when they might occur.
||Directory is protected by a password
||All visitors are barred from looking at this file or directory
||File Not Found
||Internal Server Error
||CGI script error
In our examples above, the location of the custom error page is given by a virtual path. That is to say, it starts with a slash followed by the file name, so the file should be placed in the domain root. You could also use an absolute URL to give the file's location. The file can even be a CGI script that executes when the condition occurs.
As with most things on the Web, there's a downside to using an .htaccess file. Since the server has to check for these files and process them, they introduce a slight degradation in server performance. Overall, the benefits from improving the look of your site probably outweigh the penalty.
Try using an .htaccess file on your site. After you're comfortable with simple commands like ErrorDocument, you can experiment with other directives. A full list of directives supported by Apache is available at the Apache Server Project site.