Even simpler searching

April 28, 2006 by Oliver
Filed under: Blogging, Computers, Programming, Technology, Web Programming 

nicer searching in WordPress one step further. Now if you try to get to a page that doesn’t exist it automatically redirects to the search page. So http://www.oliverbrown.me.uk/galaxia will return search results for “galaxia”.

If you want to do it yourself, you need to do two things. First you need to redirect error pages to a custom PHP page so add the following line to the top of your .htaccess file:

ErrorDocument: 404error.php

Now you need to create 404error.php and make it redirect to the search page by adding the following to it:

header('Location: http://' . $_SERVER['HTTP_HOST'] . '/search.php?s=' . $_SERVER['REDIRECT_SCRIPT_URL']);

After doing this it had me thinking about status codes. By default that will generate a 302 Found HTTP status code. A bit of reading led me to decide that 303 See Other was a better response since the request might correspond to a page in the future (and presumably a relevent page) but for now should be redirected. 3XX are somewhat confusing…

Anyway if you want that behavious, add this line too:

header('Status: 303 See Other');

Finally, you should think a little before implementing this since any random page accessed could now be cached by proxy serversand search engines etc. since the server is no longer sending a 404 Not Found code.

HTTP, status codes, WordPress]]>

Comments

Comments are closed.