404 Search function for WordPress

August 5th, 2004

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 = '';
$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 ‘’ with your blog name to get the right search results. So replace it with: ‘’
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 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.

I am still working on some tweaking, but it works well for now.
Test it out with this link. This link would normally have returned a 404 error.

Please leave comments and suggestions.




  1. Craig (19 comments.) says:


    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… :)

  2. david (3 comments.) says:

    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?


  3. Mark (7 comments.) says:

    Sorry about that. Check the post for a possible fix.

  4. Jesus (5 comments.) says:

    I prefer a 404 error page, I dont like to being redirected.
    Cheers an god work

  5. david (3 comments.) says:

    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.

  6. CarLBanks (3 comments.) says:

    I get an error.

    Parse error: parse error, unexpected ‘:’ in /home2/caldwell/public_html/carlbanks/errors/404.php on line 4

  7. david (3 comments.) says:

    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 = ''; $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(); ?>

  8. indi (2 comments.) says:

    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.

  9. Jon (1 comments.) says:

    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?

  10. Jeff (2 comments.) says:

    You’re demo link returns a 404 error ;)

  11. Mark says:

    The server change killed it. It should be operational soon.

  12. Hardik (1 comments.) says:

    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

  13. Arun (2 comments.) says:

    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


  14. Jonathan (1 comments.) says:

    Nice idea – but your own test page shows
    “The requested document was not found on this server.”!

  15. Everton (7 comments.) says:


    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.



  1. […] n, and to add a friendlier note to head off the search results. Finally, Mark Ghosh has a 404 page enhancement that redirects visitors to pages tha […]

  2. […] quer pour rendre la page de recherche un peu plus présentable. Et tout ça combiné à la fonction de recherche en cas de 404, c’est mon blau […]

  3. […] This was achieved with equal parts custom PHP (oh yes, I’m learning!) and help from WeblogToolsCollection, and Scriptygoddess. Just follow th […]

  4. Handle 404s with WordPress
    I read this article about having wordpress handle 404s. It’s a pretty popular thing to do — several of my sites implement a similar logic. But then I read the code snippet they suggest using as 404.php. The last two lines of the script just bother me…

  5. […] e, you may want to look through the following. Advanced Contextual search for WordPress 404 Search function for WordPress [via] With this don […]

  6. 404 intelligenti con wordpress

    Oggi mi sono stufato della pagina 404 di wordpress, e allora ho cominciato a indagare un po’ su come sostituirla con qualcosa di più intelligente.

    Ho preso spunto da 404 Search function for WordPress per fare in modo che al posto della pagina di err…

  7. […] started with the 404 Search from Weblog Tools Collection, which simply redirects 404 pages to search results. That’s not technically correct behaviour, so I […]

  8. […] started with the 404 Search from Weblog Tools Collection, which simply redirects 404 pages to search results. That’s not technically correct behaviour, so I […]

Obviously Powered by WordPress. © 2003-2013

page counter