Friendly Search URLs

I've recently gotten several e-mails asking how I created my friendly search URLs here on alexking.org (also used on the Crowd Favorite web site, etc.); I figured others might be interested in this as well.

It's really rather simple, there are just a couple of places to make changes. Note: you must be using nice permalinks with an Apache web server for this to work as described below.

.htaccess File

Add this mod_rewrite rule in your .htaccess file:

RewriteRule ^search/(.+)?$ /index.php?s=$1 [QSA,L]

UPDATE: Mark informs me that this has actually been built in to WP since 1.5 (meaning this step is not necessary) - who knew? Not me apparently. 🙂

Theme

Add an onsubmit handler to the form tag in the search form in your theme:

<form action="<?php bloginfo('wpurl'); ?>/index.php" method="get" onsubmit="location.href='<?php bloginfo('home'); ?>/search/' + encodeURIComponent(this.s.value).replace(/%20/g, '+'); return false;">

Then... you're done. Kinda anti-climactic, but it's a pretty simple little change.

One thing I've found handy about this is the ability to simply type:

https://alexking.org/search/your+search+terms

to search my site.

There's more than one way to skin a cat, suggestions and improvements to this are welcome in the comments.