Rewriting URL with selected query string parameters in .htaccess
.htaccess, apache, url-rewriting
Solution
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.
Problem
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.com/search.cgi?tag=foo&blog_id=bar&other=baz ``` The new URL should be: ``` http://www.mysite.com/?s=foo ``` In other words, there were several query string parameters in the old format, but I only care to rewrite the tag param to s while keeping the same value. The other parameters should be discarded. Of course the order of the parameters shouldn't matter. It also shouldn't matter whether tag is the only parameter or not. Any ideas?