[wp-hackers] Rewriting search URL

Terrence Wood tdw at funkive.com
Fri Apr 22 01:58:30 GMT 2005


Here's a proof of concept for rewriting a query string into a url... 
works on apache. I'll leave integrating it in WP for you.

1. Create a test file 'testsearch.php' and put it in your WP root dir 
with the following:

<?php
// put search term back in form if exists
$str = (isset($_REQUEST['s'])) ? 'value="'.$_REQUEST['s'].'"': null;
// always submit form to your-WP-install/search/
$self = preg_replace ( '@/search.+@', '/search/', 
$_SERVER['REQUEST_URI'] );
// note: form must be GET, POST won't work
?>
<form action="<?php echo $self; ?>" method="get">
<input type="text" name="s" <?php echo $str; ?> />
<input type="submit" value="search" />
</form>

2. Put these rules in your .htaccess above your WP rules, changing 
your-WP-install as needed:

RewriteEngine on
RewriteBase /your-WP-install/
# get the query string, append to path and redirect.
# '?' in the rewriteRule removes query string from URL
# 'R' is an external redirect so new URL appears in location bar
RewriteCond %{QUERY_STRING} s=(.+)
RewriteRule ^search/?$             search/%1/? [R]
# redirect to our search page, scraping URL for querystring if it exists
RewriteRule ^search/([^/]+)?/?$    testsearch.php?s=$1 [L]

HTH
Terrence Wood

On 22 Apr 2005, at 10:30 AM, Peak Discharge wrote:

> That changes my url into the form of http://www.unicorns.com/?s=string,
> which is a progress. The aim, however is to change all search urls to
> http://www.unicorns.com/search/string



More information about the wp-hackers mailing list