Subscribe to
Posts
Comments
NSLog(); Header Image

404 Search Function Code

A few days ago I promised that I'd post my 404 Search Function code. Here it is (below). Let me cover a few things first. Initially, I did a preg_match after having done a file(). The file() function returns an array. That's a bit costly, so I just wrap file() with an implode() to give me the whole string. I looked, and I haven't done this before so it's possible I overlooked it, but there doesn't seem to be a way to get a remote URL (the search results page) as a string in one function. Bummer.

At any rate, I'll revise this code as necessary. Submit your suggestions. Consider this code in the public domain, and use it as you see fit. If you feel like being nice, credit me. If you feel like being really nice, send me a case of Coke. If you feel like the code is crap and there's nothing to be nice about, do me the favor of telling me why.

Wrap this up in a 404.php file and set your .htaccess file to read "ErrorDocument 404 /404.php" or something.

<?
$search_term = substr($REQUEST_URI,1);
$search_term = urldecode(stripslashes($search_term) );
$search_url = 'http://blah.com/mt/mt-search.cgi?IncludeBlogs=1&search=';
$full_search_url = $search_url . $search_term;
$full_page = implode("", file($full_search_url) );
$search_string = '/<h3 class="title"><a href="([^"]*)"/';
$count = preg_match_all($search_string, $full_page, $matches);
if(1 == $count)
    header("Location: {$matches[1][0]}");
else
    header("Location: $full_page");
?>

Got questions? Check out Vinay's source or Christopher Holland's source.

40 Responses to "404 Search Function Code"

  1. 404 Search

    Erik J. Barzeski has released his 404 search function code. I have in turn, integrated it into my site. The way the code works is that if you decide that you want to visit http://www.toomuchsexy.org/FOO, and that does not exist, you will either be redi...

  2. It seems to work perfectly. Thank you so much, this is awesome.

  3. 404 search

    erik, as promised, released his 404 search function code today. i just finished uploading it, as my 404 page and

  4. Works nicely. Of course, now you realize you can add the logic in here to redirect people with old URLs to the new location ... right? :-)

    Something I think I'll be doing on my own site.

  5. design tweaks

    I've reduced the size of the fonts all around so the place was a little more friendly on smaller screens. Since most browsers can raise the size of fonts if you need them I figured it wasn't a bad tradeoff.

  6. 404 Search Function

    You can now type "kenshin.cwru.edu/searchterm or terms" and instead of giving you a 404 page saying whatever not found, you

  7. My only complaint, but I don't think you can control this.

    When someone does a search like this, MT's activity log shows the IP of the server hosting MT as doing the search since, technically, it is. It would be nice to somehow trick MT into posting the IP of the person doing the search as opposed to the host computer's IP.

  8. I predict this functionality will become extremely common on web sites. It's a no brainer.

  9. 404 Search Page...

    Erik created a wonderful 404 search function. It requires only PHP. This explains what it does. I think this one

  10. Sweet! Probably a good idea to stripslashes() on $searchterm, though: a search for what's up returns no results for what\'s up.

  11. You are right and I've added this as of, well, just now.

  12. Hi, nice work, a tried to use this for my site without MT :-)

    One remark to the value $search_string:

    it matched nothing at me, only if the <, > and " are escaped. Then it worked ;-)

  13. No more 404's

    I just implemented a very nice little PHP script for my website that ties into my site search function — the end result being that my site no longer has a "404 File Not Found" error page!

  14. OK, this is one of the coolest things I've seen in a long time. Has now been implemented on my page :)

  15. Great idea! I never considered anything like this, though I am well aware of the neatness of PHP.net's search function. Let me show you what I used, quite similar:

    $keyword = urlencode(substr($_SERVER["REQUEST_URI"], 1));

    header("Location: http://mt.irssi.dk/mt-search.cgi?IncludeBlogs=1&search=".$keyword);

    As a side note, if anyone plans to make the searching-program, make sure you urldecode whateveer's being sent before you hatch into it. Try and search for something with spaces in on PHP.net and you'll see what I mean. (Except of course foo bar as it leads directly to a very useful page, hehe -- coincidence? Oh well.)

    Best regards,

    Simon Shine

  16. Lost much

    I used NSLog();'s 404 Search Function code to redo my 404 page. If you're searching for something you can either use the search box or simply place what you're looking for in the url. Typing "http://www.lunanina.com/blah" where blah is the search word ...

  17. 404 Search Function...

    So, I'm currently working on migrating this "big-ass" web site over to a new technology and site architecture. One of

  18. Sinnlose Spielerei oder Feature?

    Ich hatte gerade mal einen Moment Zeit, und da habe ich ein kleines Feature eingebaut, das ich bei NSLog(); gefunden

  19. 404 Automatic Search

    By using Erik Barzeski’s 404 Seaerch Function, 404 errors on this web site are now redirected directly to a search and if there is only one match, the page closest resembling your query. In simpler terms, if you type in...

  20. Anyone noticed strange PHP interactions with this? If I make my 404 error page a PHP script, the REQUEST_URI when the script is evaluated points not the user's munged URL, but rather to my error page. For what it's worth, the virtual server approach my hosting service uses requires a full URL for the ErrorDocument to work; this might be the source of the problem.

  21. Turns out my problem is due to hosting service weirdness; I can only do an ErrorDocument with an absolute URL, and doing so loses the referer info.

  22. Sending 404's to search

    After some struggle and help from our hosting service, we've managed to get Eric's 404 Search hack to work here on XMLHead. So now, if you put something after the "http://www.xmlhead.com/", it will actually search through the articles for matches...

  23. A few changes

    With a modified version of Eric's 404 code (incorporating a bit of a hack from Simon Shine, I feel that I'm ready to fully incorporate my old entries. That process will probably be complete after I've returned from a trip...

  24. MovableBlog.com responds to Dave Winer's request for "Edit this Page" functionality. Good idea. If I had a place to hide that on my site, I...

  25. Searching of Entries

    I'm not sure it'll ever get used, but after viewing Erik J. Barzeski's blog and looking at his pretty clever search script I had to implement it! I thought it was done using some really clever RewriteRule in a .htaccess but instead using a clever PHP s...

  26. 404 -> Search Page

    When you try to access an address on a site that doesn't exist, you get a 404 error. What you should get is an attempt to find the page you're after and, if that doesn't work, a search page where you can enter a string of your own devising. Than...

  27. Etan, you can change one line in the code to get it to correctly search from your IP address.

    In the last line of code change:

    echo $full_page;

    to:

    header("Location: $full_search_url");

    Works for me.

    Also the script doesn't work really well if your blog directory isn't the base directory of the server, since the substr just chops the first slash off. I've edited it to handle blogs in subdirectories—or, more specifically, a certain subdirectory (in my case /mt/exordium/). I'm not exactly a PHP guru so there might be a better way—maybe just split the string by "/" and then just use the last item. Anyway, for now just change the first lines to read...

    $URI_prefix = "/mt/exordium/";

    $search_preterm = str_replace($URI_prefix,"",$_SERVER['REQUEST_URI']);

    //$search_term = substr($_SERVER['REQUEST_URI'],1);

    $search_term = stripslashes($search_preterm);

    (Also, I changed to PHP 4.3 compliant global variable names, so you may have to change it back to 'REQUEST_URI' if you don't have a new version of PHP)

  28. 404 Error Search Redux

    I noticed an entry over on Antipixel about Erik's code for redirecting "404 File Not Found" errors to a search page. Well, I looked at the code and decided that I wanted to play around with it to make it

  29. 404 code

    NSLog(); - 404 Search Function Code Redirect to blog search...

  30. 404 errors and searches

    I saw a way to re-direct 404 errors to the Movable Type search function on the site NSLog();. So I wanted to implement this on my site. My first hurdle was converting it to ASP. Here's what I came up...

  31. MovableBlog.com responds to Dave Winer's request for "Edit this Page" functionality. Good idea. If I had a place to hide that on my site, I...

  32. check out this RSSphp script

  33. Some security and scalability concerns have been raised about this solution at ScriptyGoddess. You might want to take a look at their discussion for some ideas on how to improve on this great idea.

  34. None of the points raised in the discussino at ScriptyGoddess are important. As Etan primarily points out, anyone wishing to "hurt" your server can hit up mt-search.cgi directly. Adding checks for load balancing, etc. are nice perks, but I'm also a fan of KISS. I'm already running a modified version of this script anyway.

  35. send search results instead of a 404

    i'm not convinced this is the right option for any of my sites, but just the idea of giving users' the results of a search instead of a 404 page is really great. there's gotta be something up on nelson's...

  36. Error 404 Search script

    I noticed that, Erik J. Barzeski over at NSLog(); had a cool search script written in PHP, that when you go to a URL, such as:

    http://www.forbiddenbyte.co.uk/search for me

    It searches your site. Very cool indeed. And the good thing is, is tha...

  37. I need to Search the words from a website...I need to write a search function for it... plz help me out

  38. I last wrote about the 404 Search in February 2003 (both here and here. Since that time, I've been using the 404 search code quite heavily on every site with MovableType (or any other blogging package). It's undergone some improvements,...

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


Trackback URI | Comments RSS

Leave a Reply

Please abide by the comment policy. Valid HTML includes: <blockquote><p>, <em>, <strong>, <ul>, <ol>, and <a href>. Please use the "Quote Me" functionality to quote comments.