Finding Our Way: Custom 404 Pages
It happens to all of us. No matter how much effort we spend combing through our domains looking for bad links, a visitor always finds one we've over looked first. Custom error messages can make the experience more pleasant for the wayward visitor through clever hikus, but they can also be a pretty powerful means of navigation.
Lets assume you have locally linked error 404 pages. To set it up, call something like "ErrorDocument 404 /error404.php" in your .htaccess file. Leave the path relative rather than absolute. Now when a visitor encounters a 404 error, the url remains unchanged. We can utilize the url and try to predict where the user wishes to go. The first method is by splitting the URL into search terms.
// get the path included in for query terms
function getSearchTerms(){
$file = trim(urldecode($_SERVER['REQUEST_URI']));
// get rid of extension if it exists
if (strpos($file,'.') > 0){
// split url into terms
$queryArray = explode('/',$file);
foreach($queryArray as $term){
return $query;
?>
The first term you see "site:bluecentauri.com", tells the search engine to only retrieve files on our domain. Unless this page has been moved lately, you should see the terms "code", "php" and "error". That is because the path is "code/php/error.php". When google attempts to locate the correct file, it will search the urls as well. Assuming the folders and files are not named randomly, Google should be able to find the correct file.
We can set up the query string as follows:
echo 'http://www.google.com/search?q='.urlencode(getSearchTerm());
?>
If your favorite search engine is something other than google, you'll need to modify the prefix. Remember to use urlencode to convert the search terms string into a url friendly string.
Part of the SKTyler.com Network
