htaccess redirect query string - apache

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

Related

How to redirect a URL with a querystring

I've moved a website to a new domain and I've used RewriteRule to redirect a number of my URLs.
I'm stuck trying to redirect URLs with query strings in them?
RewriteRule /news/archive.php?cat=economic-impact http://www.new-website.com/faqs/economic-impact [R=301,L]
RewriteRule /news/archive.php?cat=housing-need http://www.new-website.com/faqs/housing-need R=301,L]
RewriteRule /news/archive.php?cat=infrastructure http://www.new-website.com/faqs/infrastructure R=301,L]
Further up the htaccess file I'm using this to redirect the domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-website.com$
RewriteRule (.*)$ http://www.new-website.com/$1 [R=301]
When I visit:
old-website.com/news/archive.php?cat=economic-impact
it redirects to
new-website.com/news/archive.php?cat=economic-impact
Which 404s, I need to to redirect to:
new-website.com/faqs/economic-impact
Can anyone help?
You need to add a blank query string on your rewrite to truncate any existing query strings.
The below should do the job:
RewriteRule (.*)$ http://www.new-website.com/$1? [R=301]
In addition, the way you're checking the existing query strings won't work. You'll need to check them separately:
RewriteCond %{QUERY_STRING} "cat=(economic-impact|housing-need|infrasructure)"
RewriteRule news/archive.php http://www.new-website.com/faqs/%1? R=301,L]
The %1 will take a backreference from a regex group match in the RewriteCond

Simple Apache mod_rewrite remapping

I have to remap a few ID's to URL strings (301 redirect) and I have to do it with mod_rewrite:
/page.php?id=15 to /pagexy
/page.php?id=10 to /pageyz
The rule:
RewriteRule ^page.php?id=15$ /pagexy [L,R=301]
doesn't work. What am I doing wrong?
You need to inspect the query string separately. The following should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=15\b [NC]
RewriteRule ^/page.php$ /pagexy? [L,R=301]
RewriteCond %{QUERY_STRING} id=10\b [NC]
RewriteRule ^/page.php$ /pageyz? [L,R=301]
Given you are tying ids to pages, it may also pay to look at using a RewriteMap

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]

301 Redirect of old url with parameters to a path without parametes

I need the following request :
http://somesite.com/home.php?action=page&page_id=9
to trigger a 301 redirect to :
http://somesite.com/a-new-page
Here's what I'm using but it isn't working.
RewriteCond %{QUERY_STRING} ^page_id=9$ [NC]
RewriteRule ^home\.php$ http://somesite.com/a-new-page? [L,R=301]
Any ideas?
If the QUERY_STRING will always be the same you need to match it like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^action=page&page_id=9$ [NC]
RewriteRule ^home.php$ http://somesite.com/a-new-page? [L,R=301]
Hope this helps.

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