This is a modification of the 404 function found at NSLog. This is tweaked such that if a page is not found on your WordPress blog, the part of the URI beyond the domain name is stripped off and fed into the WordPress search function. If only one post if found, that post is directly loaded. Otherwise the whole list of posts is loaded and displayed.
Installation is really simple. Just copy the code below and paste it into a file called 404.php. Then open up the .htaccess in the root of your blog and add the following on a new line:
ErrorDocument 404 /404.php
/*Code for 404.php*/
$search_term = substr($_SERVER['REQUEST_URI'],1);
$search_term = urldecode(stripslashes($search_term));
$search_url = 'http://weblogtoolscollection.com/index.php?s=';
$full_search_url = $search_url . $search_term;
$full_search_url = preg_replace('/ /', '%20', $full_search_url);
$full_page = implode("", file($full_search_url));
EDIT: Replace the ‘http://weblogtoolscollection.com/index.php?s=’ with your blog name to get the right search results. So replace it with: ‘http://yourdomain.com/yourblog/index.php?s=’
Also, if your blog is within a folder (or folders), the following will prevent some weird results. Count the number of characters (including the first slash after your domain name) after your domain name till the end of the foldername of your blog. So if your blog is located at http://mindfulmusings.net/weblog/ you would count 8 characters. Now, replace the number 1 in $search_term=substr($_SERVER[‘REQUEST_URI’],1); with that number. I know this is a little kludgy. I will come up with a more elegant solution soon.
Test it out with this link. This link would normally have returned a 404 error.
I am still working on some tweaking, but it works well for now.
Please leave comments and suggestions.