1

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?

2
  • For starters, your initial URL does not have a slash after player-search, so demanding one in your pattern will make it not even match what was requested … Commented Feb 11, 2015 at 22:27
  • Ok, updated htaccess (and updated my question) but it still doesn't work... Commented Feb 11, 2015 at 22:42

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.