PHP and Smart 404's

Using custom 404 handlers isn't new. Most decent web development companies will as a matter of course use the layout of a wqebsite design to implement a 404 page that maintains the look and feel.
One trick to take the 404 to the next level is building in some smarts. In a recent exercise, I utilized a very little-known function in PHP to give a 404 some useful functionality.
At the heart of these smarts - the sitemap.xml file and PHP's levenshtein function.
http://www.php.net/levenshtein
The levenshtein function uses a unique algorithm to find the reletive proximity of 2 strings by dtermining how many insert, remove and replace operations would be needed to transform the first string in the arguments to the second.
For example:
If you have the strings contact_us.php and contact-us.php, these strings would have a levenshtein distance of 1. That is, it would take one operation, the replacement of the underscore with the hyphen, to transform the first string into the second.
How is this handy?
Let's say you had a file on your site called contact_us.php and you realized that a hyphen was better. Sure you'd add the 301 in there for a while but eventually you'd have to remove it, otheriwse you'd have to keep every iteration of your website on the server, which would become cluttered pretty quickly. Once the file was removed, someone who had the old file bookmarked would get the 404 page. In the 404 page you have the name of the requested file available to you. Using the sitemap.xml your function can traverse the hierarchy of your entire website and calculate the levenshtein distance between the requested page and each page on your site. In our example above, contact-us.php would be at the top of the heap and worthy of either a "did you mean". If there were no others even close you might even consider using a header to forward the visitor to the new page.
404's don't have to be just pretty. They can and should be functional. Not a lot of work, plenty of wow factor.
Mike
One trick to take the 404 to the next level is building in some smarts. In a recent exercise, I utilized a very little-known function in PHP to give a 404 some useful functionality.
At the heart of these smarts - the sitemap.xml file and PHP's levenshtein function.
http://www.php.net/levenshtein
The levenshtein function uses a unique algorithm to find the reletive proximity of 2 strings by dtermining how many insert, remove and replace operations would be needed to transform the first string in the arguments to the second.
For example:
If you have the strings contact_us.php and contact-us.php, these strings would have a levenshtein distance of 1. That is, it would take one operation, the replacement of the underscore with the hyphen, to transform the first string into the second.
How is this handy?
Let's say you had a file on your site called contact_us.php and you realized that a hyphen was better. Sure you'd add the 301 in there for a while but eventually you'd have to remove it, otheriwse you'd have to keep every iteration of your website on the server, which would become cluttered pretty quickly. Once the file was removed, someone who had the old file bookmarked would get the 404 page. In the 404 page you have the name of the requested file available to you. Using the sitemap.xml your function can traverse the hierarchy of your entire website and calculate the levenshtein distance between the requested page and each page on your site. In our example above, contact-us.php would be at the top of the heap and worthy of either a "did you mean". If there were no others even close you might even consider using a header to forward the visitor to the new page.
404's don't have to be just pretty. They can and should be functional. Not a lot of work, plenty of wow factor.
Mike