colourcolourcolourcolourcolourcolourcolour
Forgot my colour selection

Pretty Search In WordPress

Sure, you can enable the fancy blog post url’s with the .htaccess file, but when someone uses the search box WordPress still sends the user to http://www.blogname.com/?s=Foobar, whatever you have your permalink structure set as. To get around this, i’ve used my own PHP file to redirect the user’s search to http://www.blogname.com/search/Foobar/.

I created a file that I just dropped into the root of my blog directory called “pretty-search.php”. This is the code it uses:

<?php $term = $_GET['s']; header(”Location: http://www.blogname.com/search/$term/”); ?>

This simple bit of code changes the user’s search request that would originally be http://www.blogname.com/?s=Foobar to http://www.blogname.com/search/Foobar/, to make the URL look better.

To make the script actually work though, I had to also point my searchform in the sidebar to the redirecting file. So, this can’t be done if the search is a widget, as it is difficult to access the code.

<form method="get" id="searchform" action="/pretty-search.php">
<input type="text" value="Search ” name=”s” id=”s” />
</form>

When you’ve sorted that bit out, you’re done. If you want to see a demo of the script in action, just type something in the search form at the top of the sidebar and hit enter. Remember, change the URL in pretty-search.php to your own blog url, currently it’s set to “http://www.blogname.com/”. ;)

pretty-search.php - 87b

Posted on February 24th, 2008. Filed under Awesome, Code, Internet, and tagged with , , , .

Leave a Comment

Make yourself heard

Montly Post Archives

Posts from previous months