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));
print_r($full_page); die();
?>
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.
http://weblogtoolscollection.com/staticise
Please leave comments and suggestions.
I have my blog in /blog/. When I enter /blog/super/ (doesn’t exist), I get the following returned: » Home > Search Results for: blog/super. How can I change this?
Thanks
Sorry about that. Check the post for a possible fix.
Don’t worry.. I got it. Here is the code I use.
< ? $URI_prefix = "/blog/"; $search_preterm = str_replace($URI_prefix,"",$_SERVER['REQUEST_URI']); $search_term = substr($_SERVER['REQUEST_URI'],1); $search_term = stripslashes($search_preterm); $search_term = urldecode(stripslashes($search_term)); $search_url = 'http://davidblog.ws/blog/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)); print_r($full_page); die(); ?>
>
I get an error.
Parse error: parse error, unexpected ‘:’ in /home2/caldwell/public_html/carlbanks/errors/404.php on line 4
WP comments love to delete anything between code tags.. sorry.
I will post code (and the answer to your problem CarL) in the wordpress forum topic.
I prefer a 404 error page, I dont like to being redirected.
http://eltintero.net/weblog/404.php
Cheers an god work
Jesus,
When I clicked your link, I was taken to a page which redirected me to your 404 page! I thought you didn’t like redirects? 😉 Sorry, bud…couldn’t resist… 🙂
You’re demo link returns a 404 error 😉
The server change killed it. It should be operational soon.
Thanks a lot for this tool. I left TypePad and I thought that I’d lost all that traffic, but this redirects it all to the relevant post on my new site. As a note, copying and pasting the text from here gave me an ‘unexpected “:”‘ error and I had to copy it from the WordPress Forum. As another note, I added the line
$full_search_url = preg_replace(‘/_/’, ‘%20’, $full_search_url);
to strip out the underscores TypePad inserts, if anyone else is using it for that purpose. Anyways, thanks a lot for building this community.
Is there a .shtml format for that? My current host doesn’t allow me to use a .php extension for my error pages so I can’t use that 404 code above. Any ideas?
Hello, I want built my site . I need to have a search function at the HOME Page. Can we write the search code in ASP ? I would like to see that as well, plz reply me at hardik_1315@yahoo.com
404 Error Page With Deafult Search Results
Hi nice post. I just did some research on building a good 404 page in WordPress. check out my post at WordPress 404 Error Page Tutorial
-Arun
Nice idea – but your own test page shows
“The requested document was not found on this server.”!
Hi
I’ve just moved from blogware to WP and I’m trying to get my head around this. can I ask two questions please:
1. when you say:
Then open up the .htaccess in the root of your blog and add the following on a new line: ErrorDocument 404 /404.php
Where exactly do I find the .htaccess file? I can’t see it in my file manager
2. In which folder should I save the 404.php file? In – blog/404.php?
thanks in advance for any help.
Everton