8/5/2004 ↓

404 Search function for WordPress

If you like this post, please subscribe to our RSS feed to read our new posts every day.

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.

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.
http://weblogtoolscollection.com/staticise

Please leave comments and suggestions.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Friends

Translate

Translate to German Translate to Spanish Translate to French Translate to Italian Translate to Portuguese Translate to Japanese Translate to Korean Translate to Russian Translate to Chinese

Latest Videos

25 Comments | Leave a comment | Comments RSS

  1. 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

    [Reply]

    david (3 comments.) — 08/5/2004 @ 11:49 am
  2. Sorry about that. Check the post for a possible fix.

    [Reply]

    Mark (7 comments.) — 08/5/2004 @ 12:01 pm
  3. 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();
    ?>

    [Reply]

    david (3 comments.) — 08/5/2004 @ 12:09 pm
  4. I get an error.

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

    [Reply]

    CarLBanks (3 comments.) — 08/5/2004 @ 12:11 pm
  5. 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.

    [Reply]

    david (3 comments.) — 08/5/2004 @ 12:15 pm
  6. I prefer a 404 error page, I dont like to being redirected.
    http://eltintero.net/weblog/404.php
    Cheers an god work

    [Reply]

    Jesus (5 comments.) — 08/5/2004 @ 9:34 pm
  7. 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… :)

    [Reply]

    Craig (19 comments.) — 08/7/2004 @ 11:03 pm
  8. [...] 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 [...]

    wordlog.com » Improving the Search Result Page — 08/12/2004 @ 5:49 am
  9. [...] 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 [...]

    Le Blaugue à Beleg | Chercher! — 08/12/2004 @ 1:53 pm
  10. [...] « | Home 9/3/2004

    404 Search function for WordPress
    (PHP, WP) Carla @ [...]

    bookmarks » — 09/3/2004 @ 11:53 am
  11. You’re demo link returns a 404 error ;)

    [Reply]

    Jeff (2 comments.) — 09/21/2004 @ 9:17 pm
  12. The server change killed it. It should be operational soon.

    [Reply]

    Mark — 09/22/2004 @ 9:10 am
  13. 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.

    [Reply]

    indi (2 comments.) — 09/26/2004 @ 3:46 am
  14. [...] This was achieved with equal parts custom PHP (oh yes, I’m learning!) and help from WeblogToolsCollection, and Scriptygoddess. Just follow th [...]

    noscope | Photography and 404 Updates — 09/28/2004 @ 8:10 pm
  15. 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?

    [Reply]

    Jon (1 comments.) — 10/11/2004 @ 8:51 pm
  16. 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…

    marcus' scrappad — 11/9/2004 @ 10:23 pm
  17. 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

    [Reply]

    Hardik (1 comments.) — 12/24/2004 @ 8:36 am
  18. [...] e, you may want to look through the following. Advanced Contextual search for WordPress 404 Search function for Wordpress [via] With this don [...]

    noscope | Providing a separate page of Search Results with Wordpress — 04/20/2005 @ 3:57 am
  19. 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…

    Naufraghi nella rete — 02/5/2006 @ 3:11 pm
  20. 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

    [Reply]

    Arun (2 comments.) — 03/6/2006 @ 1:55 pm
  21. Nice idea - but your own test page shows
    “The requested document was not found on this server.”!

    [Reply]

    Jonathan (1 comments.) — 03/12/2006 @ 7:44 am
  22. 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

    [Reply]

    Everton (7 comments.) — 06/2/2006 @ 1:14 pm
  23. [...] 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 [...]

    archGFX | Adding Search results to Wordpress 404 pages — 09/12/2007 @ 1:20 pm
  24. [...] 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 [...]

    archGFX Habari — 01/12/2008 @ 1:33 pm

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(required)

(required, will not be published)


S2