Blogging, Computers, Programming, Technology, Web Programming

Even simpler searching

<![CDATA[I decided to take my idea for 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]]>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s