The Weblog of Erik J. Barzeski

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.

Like this:

40 Responses to "404 Search Function Code"

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

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.

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.

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

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

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

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.

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

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

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

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

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

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

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

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.

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

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