Go to content Go to navigation Go to search

Fun with .htaccess #1: Special Error Pages

It is possible to arrange for a special page to be displayed if an error occurs on the server, for example, if a file is not found.

As with most things in this section, this relies on using a .htaccess file. The following file specifies what page to display if a file is not found (error 404) and what to happens if a CGI script, say, for a mailing list, does not execute successfully (error 500). These are the most common webserver errors.

.htaccess

ErrorDocument 404 /home/lawfac/web/file_not_found.shtml
ErrorDocument 500 /home/lawfac/web/internal_error.shtml

Each line specifies the location on the file-system (not the webserver!) of the file to be displayed if the error occurs. In the above setup the file /home/lawfac/web/file_not_found.shtml is displayed if a File Not Found error occurs. This has error code 404. The only other error that's likely to happen is a 500 Internal Error, usually triggered when a CGI script goes awry. For a full list of HTTP error codes see this site.

What happens when a file is not found then? First the server looks in the folder the user gave in the URL for a .htaccess file. If it finds it, and it contains an ErrorDocument line, it displays the given file, otherwise it looks in the next folder up. If it goes right up through the folder tree to the root folder without finding such a file, it displays a file given in the server's main configuration file.

One of the more intriguing consequences of this is that the user can specify different error files depending on where the error occurred. For example, there could be one .htaccess file in the site's folder specifying a general File Not Found page, and then another could be placed in the folder containing, say, event pages, displaying a different page if an event page wasn't found (for example, if a bunch of events were cancelled due to logistical difficulties, a special page could be arranged to explain the missing links).