fettig.net

A custom 404 handler for pages that have moved

Posted by Abe on Wednesday, October 23, 2002 @ 4:22 pm

Yesterday I did something I’ve been meaning to for almost a year: built a custom 404 handler to help people find pages on this site that have moved.


First I added a line to my .htaccess file:

ErrorDocument 404 /404.py

This tells Apache to use the script "404.py" in the root directory of my site to handle 404 (File Not Found) errors.


Then I uploaded 404.py, a python cgi script.  It contains a dictionary that maps files that have moved to their new locations.  If it can find the page they were looking for in the dictionary, it redirects their browser to the new page location (for an example, try the old Hep page: http://www.fettig.net/projects/000050.html).


If the page isn’t listed in the dictionary, 404.py prints out a "Not found" page (example).


The motivation for me to do this was a look at my web server’s error log, which revealed that I still get hits on /projects.py, my old project page, which has been gone for almost a year.  Now those people will be redirected to my current projects page.  And going forward I won’t have to leave old pages around forever just to avoid broken links.


For those who are interested, the code is here.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.