user2265915
user2265915

Reputation: 559

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?

Upvotes: 1

Views: 1325

Answers (2)

anubhava
anubhava

Reputation: 784958

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

Upvotes: 1

Panama Jack
Panama Jack

Reputation: 24448

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]

Upvotes: 1

Related Questions