I recently changed my CMS, and want to rewrite some of my URLs to match the new URL/query string parameter format.
The old URL was:
http://www.mysite
Here is another version to get specifically what the OP meant. @smhmic's answer is close to this. I just prefer explicit beginning-or-ampersand to find the parameter, rather than the word-boundary approach. (The accepted answer is overly complicated, and has some issues.)
RewriteCond %{QUERY_STRING} (?:^|&)tag=([^&]*)
RewriteRule ^search\.cgi$ /?s=%1 [NC,R=302]
This is saying:
(?: )
don't capture this as a % number
^|&
at beginning of querystring or after an ampersand (this acts like the word boundary, but specifically for urls).
tag=
the param we're looking for.
( )
capture this, into %1.
[^&]*
zero or more characters not an ampersand (and stopping at ampersand or running to the end).
You can try to use regular expression and grouping, if your server supports that. If I am not mistaken you have to rewrite only one parameter, I guess you could try something like (if you are using apache with mod_rewrite):
RewriteCond %{QUERY_STRING} ^.*(\btag\b=(\w+|&\w+;)+) RewriteRule ^(.+) /$1?%1 [L]
Edit: I improved the regular expression a little bit to match "tag" regardless of its position in the query string and to preserve special characters sequences such as &
In addition it should avoid matches with similar parameters (i.e.: it wouldn't match a parameter called "alttag").
Edit #2: An alternative (especially if you have to filter several parameters) is to use an external program to do the rewrite. This page: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap (in particular the section "External rewriting program") contains useful information.
On an Apache webserver, placing the following rule into .htaccess in your document root will rewrite URLs as you described:
RewriteCond %{QUERY_STRING} .*\btag=([^&]*).*
RewriteRule ^search\.cgi /?s=%1 [R=302]
The rule above uses a 302 redirect, so your browser doesn't "memorize" the redirect rule while you're testing and tweaking the rule. Once you're done testing, change the [R] to [R=301]. A 301 is a permanent redirect, and is better for SEO.