htaccess redirect query string and discard

Tag: .htaccess Author: SunSetStar Date: 2009-11-04

Having trouble with redirecting a dynamic URL. This is what I want to accomplish:

Redirect 301 /content/index.php?id=423

I tried this

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=423$
RewriteRule ^content/index\.php$ [L,R=301]

but had no luck. Thank you! ps/ I lost some para work with this code

RewriteRule ^([^.]+)/([A-Za-z]*)-([^.]+)-([0-9]+).html$ $1/$4-$2-$3.html [L,R=301]

Best Answer

You need to specify the query in the replacement URL. Otherwise the original query is taken:

RewriteCond %{QUERY_STRING} ^id=423$
RewriteRule ^content/index\.php$ [L,R=301]

And if you want to preserve other URL arguments, try this:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=423&*([^&].*)?$
RewriteRule ^content/index\.php$ [L,R=301]