RewriteRule to disregard a url variable - apache

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.

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]

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

Related

Appending to query string with everything intact

I tried the below. That doesn't seem to work.
If the referrer has the string google.com, I want the rewrite rule to trigger, and preserve the existing query string and just append the referrer to the end of the query string
RewriteEngine On
RewriteCond %{HTTP_REFERER} .*google.com.*$
RewriteRule ^(.*)$ ^(.*)$referrer=%{HTTP_REFERER} [QSA]
I'm sure something in my rewriterule is wrong. Can someone point me in the right direction?
This should be better for you:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)referrer=
RewriteCond %{HTTP_REFERER} google\.com
RewriteRule ^(.*)$ $1?referrer=%{HTTP_REFERER} [QSA]
This worked
RewriteEngine On
RewriteCond %{HTTP_REFERER} google\.com
RewriteCond %{QUERY_STRING} !^referrer=(.*)$ [NC]
RewriteRule ^(.*)$ $1?referrer=%{HTTP_REFERER} [L,R,QSA]
I needed to add a condition that if there is already a referrer query string parameter, don't append it. Else I was getting an infinite loop.
And I needed a external Redirect flag. Else it was not redirecting.

htaccess redirect query string

How can I redirect a specific query to another URL in htaccess? The URL is:
http://miniwars.co.uk/player-search?location=&distance_max=&category%5B%5D=epic
The location, distance_max and category are always present, and I only want to redirect when location, distance_max are empty, and category is "epic".
I want to redirect to:
http://miniwars.co.uk/epic-players
The htaccess rule I'm trying is:
RewriteCond %{QUERY_STRING} ^(category=epic|location=|distance_max=)($|&)
RewriteRule ^player-search$ miniwars.co.uk/epic-players/ [L,R=301]
...But it's not doing anything. Can someone let me know where I've gone wrong?
Maybe you should try it like this.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(location=&distance_max=&category=epic)$ [NC]
RewriteRule ^player-search /epic-players/ [L,R=301]
I only want to redirect when location, distance_max are empty, and category is "epic".
You can use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)location=(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)distance_max=(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)category=epic(&|$) [NC]
RewriteRule ^player-search/?$ /epic-players/? [L,R=301,NC]
This will allow your query parameters in any order
? in the target URI will strip off previous query string

redirect from specific request

I have something like this:
RewriteCond %{QUERY_STRING} ^something=true$
RewriteRule ^$ http://www.a123dress.com/ [R=301,L]
I want redirect ALL (*) request which have "?something=true" to
http://www.a123dress.com/
It works for:
http://www.a123dress.com/?something=true
I want rewrite for example:
http://www.a123dress.com/test/?something=true
to:
http://www.a123dress.com/test/
so cut off ?something=true like above in first example
Your regex ^$ is just allowing home page / to be redirected. Change your rule to:
RewriteCond %{QUERY_STRING} ^something=true$ [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L,NE]

redirect url with query string to path, and url without query string must be internally rewritten

I've been trying and trying.
If one goes to:
www.domain.nl/vereniging
internally a page is requested from:
www.domain.nl/?p=vereniging
For that I use this:
RewriteCond %{QUERY_STRING} !(p=.*)$
RewriteRule ^(.+)$ ?p=$1 [NC]
If a users visits:
www.domain.nl/?p=vereniging
I want the users to be redirected to:
www.domain.nl/vereniging
For that I use:
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule ^$ http://www.domain.nl/%1? [NC,R=301]
(If I put RewriteCond %{REQUEST_FILENAME} !-d before this, it doesn't redirect anymore. That's strange because a query is not a directory right?)
Separately, these 2 chunks of code work.
However, if I put them together in 1 .htaccess it bitches about looping.I don't understand this, because the conditions should prevent looping.
Try applying the END flag to either the first or second RewriteRule.
Look at the END flag here: http://httpd.apache.org/docs/current/rewrite/flags.html
You need to check against the actual request:
RewriteCond %{THE_REQUEST} \?p=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]

Strip specific parameteters when redirecting with Mod-Rewrite

I have a pretty complex RewriteRule where I need to check if certain parameters are present in QueryString and then redirect to the same URL but with those parameters stripped.
How can I remove some parameters and preserve the rest?
RewriteCond %{QUERY_STRING} color=red
RewriteCond %{QUERY_STRING} status=contiue
RewriteRule ^(.*)$ /$1? [R=301,L]
url is like:
"http://example.com/site.php?setup=done&color=red&weight=100&status=continue"
(parameters order and quantity is not predictable/hardcoded)
Try these rules:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)(color=red|status=continue)($|&)(.*)
RewriteRule .* $0?%1%5 [N,E=REMOVED:true]
RewriteCond %{ENV:REMOVED} true
RewriteRule ^ %{REQUEST_URI} [L,R=301]
Another way would be to use PHP to check what parameters are given and remove them there.