Even simpler searching
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 relevant page) but for now should be redirected. 3XX
are somewhat confusing.
Anyway if you want that behaviour, 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 servers and search engines etc. since the server is no longer sending a 404 Not Found
code.