views:

40

answers:

2

I have some pages indexed by Google, for example:

/product.html?affiliateid=142

I want a rewrite rule to 301 redirect to the same page if there's an affiliateid=xxx

So far I have this:

RewriteCond %{QUERY_STRING}  ^affiliateid=[0-9]+$
RewriteRule ^$ /test.html$ [L,R=301]

But its not working, I need to get rid of the variable and get the page name somehow.

A: 

You need to specify an empty query in your substitution URL to have the original requested query not appended to the new URL:

RewriteCond %{QUERY_STRING} ^affiliateid=[0-9]+$
RewriteRule ^ %{REQUEST_URI}? [L,R=301]

And if you want to preserve any other query parameter, try this:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)affiliateid=[0-9]+(&+(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%4 [L,R=301]
Gumbo
A: 

Found it:

RewriteCond %{QUERY_STRING} ^affiliateid=([0-9]+)$

RewriteRule ^(.*)$ /$1? [L,NC,R=301]

It's that question mark here /$1? that tells the rule to end the rewrite at the query string

tridat