January 16, 2004

404!

The latest version of A List Apart features Ian Lloyd's article on creating "The Perfect 404." The article outlines a number of useful ideas and strategies, complete with plenty of JavaScript code for the taking.

One approach that he didn't mention is to automatically redirect users to the correct page. This takes some server-side programming, database work, and elbow grease, but it could be quite useful for site redesigns.

First, you need a database (MySQL would be a nice choice) table that includes both the old and new urls. In other words, if you moved had a page at /foo/bar.html and you moved it to /bar/foo.html, your table would include look like this:

old new
/foo/bar.html /bar/foo.html

Then, in your scripting language of choice (ie, PHP, ASP, CF, Perl, Python, etc.), add code to your 404 page that hits the database looking for the offending page. If it returns a new page, then just redirect the user directly to that page. If you really want to be nifty, send along HTTP code 301 (Moved Permanently).