404 Search

I found this neat script over at NSLog via Antipixel

Basically it is a snippet of PHP that you use in place of a 404. The php reads out what the user had looked for (and failed) and submits that information to the MoveableType search CGI. If more than one result is found, then the complete search page is listed. However, if only a single result is found you are immediately forwarded to that page.

I liked the idea so I made a few modifications to broaden its abilities. First I altered $REQUEST_URI to the more prefered $_SERVER[“REQUEST_URI”]. Second I redid the bracketing ;) It’s a habit of mine, I mean no disrespect :)

Finally I added what I called a $translation_array. This array lets you specify specific keywords that you’d like to handle on a one by one basis. For example, I redid my archiving format a while back. /archives/cat_php.html is now located at /archives/php/. Therefore if a user tries to access /archives/cat_php.html (and they’ll fail) they should be forwarded to the new location.

The code steps through this array and checks to see if the failed uri contains any of the $old values. If so the $new value is assigned to the $forward variable. Once all of the translations have been checked, if any matches were made the script goes ahead and forwards to the new address. Else, we go to Erik’s code and do the MT search dance. Here is my updated version of the script:

$translation_array = Array(archives/cat_php.html=>/archives/php/,
						   archives/cat_music.html=>/archives/music/ );

$search_term = substr($_SERVER[REQUEST_URI],1);

foreach($translation_array as $old=>$new){
	if(strpos($search_term,$old) !== false)
  		$forward = $new;

	header(Location: $forward);
	$search_term = stripslashes($search_term);
	$search_url = '/error.html;
	$full_search_url = $search_url . $search_term;
	$full_page = implode(”“, file($full_search_url));
	$search_string =