Ethan Rævan
Ethan Rævan

Reputation: 333

htaccess: rewrite query string to path

I have a domain with the following query string:

http://example.com/?taxo=question&cata=foo&catb=bar&catc=more

My objective is to convert this query string after ?taxo= into a path based on the values for each parameter like so:

http://example.com/questions/cata/foo/catb/bar/catc/more/

Using .htaccess, this is what I've tried:

RewriteCond %{QUERY_STRING}
RewriteRule ^taxo=(.*)&(.*)=(.*)&(.*)=(.*)&(.*)=(.*)$  $1/$2/$3/$4/$5/$6/$7 [L,R=301]

However, that doesn't work for me. I'm trying to understand where I am going wrong with the RewriteRule line since the condition isn't being met.

Upvotes: 1

Views: 865

Answers (1)

anubhava
anubhava

Reputation: 784968

You cannot match query string in a RewriteRule. You should use QUERY_STRING variable in a RewriteCond to match and capture value from query string:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^taxo=([^&]+)&([^=]+)=([^&]+)&([^=]+)=([^&]+)&([^=]+)=(.*)$ [NC]
RewriteRule ^/?$ /%1/%2/%3/%4/%5/%6/%7? [L,R=301]
  • Note that you will need to use %N syntax for back-reference of values captured in RewriteCond.
  • ? at the end of the target URI is to strip off previous query string.

Upvotes: 1

Related Questions