I’ve been working on a 404 error page for the theme that I use here, Subtle.
I wanted to add error page support to the theme so I could utilise the error tracker plugin for Mint and also get search engines to adjust their listings for the website.
I wrote a funky bit of PHP code, which was inspired by A List Aparts: The Perfect 404 post.
What it does is look for a referral from a listed search engine and then get the keywords used in the search. It then assigns those keywords to the variable $new_search . That variable can then be used to populate your own search form.
< ?php
// Here we do some intelligent search engine 404 checking
// This allows us to show some links to possible relevant content
if(strlen($_SERVER['HTTP_REFERER']) > 0) {
// We have a referer value somewhere
// Look for a search engine query
$engines = array('google.co', 'looksmart.co', 'altavista.co', 'msn.co', 'yahoo.co');
for($I = 0; $I < count($engines); $I++) {
$found = strstr($_SERVER['HTTP_REFERER'], $engines[$I]);
}
if(strlen($found) > 0) {
// We have a search engine..
// Split the referal up to get query used
$query = explode('?', $found);
$parts = explode('&', $query[1]);
$part_count = count($parts);
// Find specific search terms based on differing search engines
$style = array('q=', 'p=', 'ask=', 'key=');
$style_count = count($style);
// start by looping through the URL parts
for($s = 0; $s < $part_count; $s++) {
// Match url parts against search query types
for($t = 0; $t < $style_count; $t++) {
$terms = strstr($parts[$s], $style[$t]);
if($terms == true) {
// finish loop as we've found what we need
$s += $part_count;
$t += $style_count;
$terms = explode('=', $terms);
}
}
}
// Search terms to echo in the search box
$new_search = str_replace('+', ' ', $terms[1]);
}
}
?>
I’m sure the code could be tidied up a bit, but it does its job. Feel free to use it for your purposes!