How to make a local Error Handler

Visitors may try to load files in your directory which don’t exist. This may be due to a bad link in your site, but more likely the error is located in a search engine or a link on an external site or elsewhere on MIA.

You need to capture that attempt and tell the visitor how to find the file they're looking for and give them the opportunity to tell you about it. This is done more effectively the closer to the location of the non-existent file that you can capture the attempt.

An attempt to load a non-existent file will generate an error code 404 in the server. The server responds by searching up the directory tree until it finds a file called .htaccess (extension without filename). The .htaccess file is an ordinary text file which contains either of two types of redirection instruction.

See Tutorial on .htaccess for a comprehensive explanation.

Example 1: http://www.marxists.org/archive/marx/.htaccess:

ErrorDocument 404 http://www.marxists.org/archive/marx/404.htm

Example 1 redirects a client which tried to load any non-existent file in the directory or subdirectories to the local error-handler 404.htm

The 404.htm is an ordinary html document. See http://www.marxists.org/archive/marx/404.htm as an example.

This should contain the message explaining that the file was not found, links to indexes which you believe will reliably lead the visitor to any information in the relevant directory, and an email-button to the director.

Note: You cannot use a relative address for the destination diectory; it must be an absolute address http://www.marxists.org/...

Example 2: http://www.marxists.org/archive/marx/works/.htaccess:

Redirect /archive/marx/works/1840/com-man/partone.htm http://www.marxists.org/archive/marx/works/1848/communist-manifesto/ch01.htm

Example 2 redirects a client which tried to load works/1840/com-man/partone.htm (an old file which has been renamed and relocated, and may or may not exist) to works/1848/communist-manifesto/ch01.htm. You can have multiple lines like this.

This is the preferred method. Once you have created the .htaccess file in a parent directory somewhere, it will handle all old links and you can delete the old files and directories and forget about them. Including an ErrorDocument 404 line at the end of the .htaccess file, after the specific redirects, will deal with “others”.

When the old files are deleted, any existing links elsewhere in the archive will generate error messages on allbadrefs.htm, and these can then be removed.

Example 3: http://www.marxists.org/archive/marx/works/.htaccess:

Redirect /archive/marx/works/1840/com-man http://www.marxists.org/archive/marx/works/1848/communist-manifesto

Example 3 lists only the directory, not the filename, and redirects a client looking for works/1840/com-man/foo.htm (an old file which has been relocated) to works/1848/communist-manifesto/foo.htm.

This is easier when you have a lot of files in the old directory, but is not recommended because if you change the names of files in the new location, the client will get an error message when redirected there, but you will have no record of the old file names. This means that it is difficult to maintain and impossible to check.


Example 4: The same effect can be achieved by placing a redirect command inside the redundant file. This avoids generating bad link messages which you would get if you deleted the old file, but it is not the preferred method, as it leaves redundant files around the archive, whch will have to be cleaned up some time, and fails to alert others administrators of the need to update their links.

To do this, an additional line of code must be placed in the header of an html file (http://www.marxists.org/archive/marx/works/1840/com-man/index.htm):

<META http-equiv="Refresh” content="3;URL=../../1848/communist-manifesto/index.htm">

The “3” here tells the client to wait 3 seconds before moving to the URL.