Tuesday, January 31, 2006 Searching 404 in PHP tweako

If your website has a search page, it can be helpful to redirect all 404 errors to the search page so the user can use it to find what they are looking for. But that requires they retype in the URL or search string. If you could do this for the user and give them the results without them having to type anything in, it will save them time, and increase the chances they find what they are looking for and stay on your site.

I'm using the freely available Fluid Dynamics Search Engine for my examples, so my search page is search.pl but just simply change the $search_page variable to meet your needs. You can hard code your page URL, or, if you think you may use the same code on a few different servers, make it more dynamic with the predefined variable $_SERVER['HTTP_HOST'].

Some php functions you will need to use are header() and basename() so we will explain how to use them first.

The header() function can be pretty complex, but for our use it is very straightforward. Once we have the url we want to direct the user to, namely, our search page plus search terms, we use the function to send them there. The argument is the string "Location: " and the url you want to direct them to. Make sure nothing appears in your actual output page before the function is called, or it won't work, like our second example.

404.php
<?php
   $search_page = 'http://' . $_SERVER['HTTP_HOST'] . '/search/search.pl';
   header("Location: $search_page");
?>
broken404.php
<!-- Redirect to Search Page -->
<?php
   $search_page = 'http://' . $_SERVER['HTTP_HOST'] . '/search/search.pl';
   header("Location: $search_page");
?>

Observe in the second example how a simple comment in our output page prevents the header() function from working.

Now we want to grab the page name the user is searching for, so first we check to see if there is a redirect page, then grab the pagename. The predefined variable $_SERVER['REDIRECT_URL'] contains our url if there is one, so we use the isset() function to see if it contains a value.

Now this page url can contain a long path if our site contains any levels of depth, and we may want a more broad search through our site. Also we may have changed our site from one scripting language to another, or from static html pages to dynamic ColdFusion pages. Thankfully we can simply use the basename() function to strip off the path and extension, leaving us with just the filename.

Just add that filename to our search page string, and now we're performing the search automatically when redirected.

404.php
<?php
   if (isset($_SERVER['REDIRECT_URL'])) {
		$search_page .= "?Terms=" . basename($_SERVER['REDIRECT_URL'], ".htm");
	}
   $search_page = 'http://' . $_SERVER['HTTP_HOST'] . '/search/search.pl';
   header("Location: $search_page");
?>

And that's all there is to it. Quick, simple, and time saving for your users.