redirect from specific request - apache

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]

Related

Apache redirect & rewrite

This is my page
http://example.com/index.html .
I would like to make it work like this.
I would like to take the user to http://def.com when he requests
http://example.com/index.html
Even if he requests http://example.com/index.html?headers=0, he should be taken
to http://def.com
But if he requests the same URL with other query parameters, say for example http://example.com/index.html?footer=1, then the redirect or rewrite shouldn't happen. He should still be seeing the response from http://example.com/index.html?footer=1
I have already tried
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/index.html$ http://def.com
Change your htaccess file to this:
RewriteCond %{QUERY_STRING} ^(headers=0|)$ [NC]
RewriteRule ^index.html$ http://def.com [R=301,L]
Change your htaccess file to this:
RewriteCond %{QUERY_STRING} ^$ [OR]
RewriteCond %{QUERY_STRING} !headers
RewriteRule ^index.html$ http://def.com [R=301,L]

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

How to do a specific condition for escaped_fragment with rewrite rule in .htaccess

I have urls like that /#!/page1, I redirect them with :
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F(.*)$
RewriteRule ^$ /seo/%1.html [QSA,L]
So /#!/page1 read as /_escaped_fragment_=%2Fpage1 is redirected to /seo/page1.html
It's work perfectly however I want to redirect the home /#!/ to /seo/index.html which actually redirect to /seo/.html
How can I do that?
Thanks
I fixed it with:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F$
RewriteRule ^$ /seo/index.html [QSA,L]
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F(.*)$
RewriteRule ^$ /seo/%1.html [QSA,L]

RewriteRule to disregard a url variable

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

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.