werd
werd

Reputation: 646

Rewrite rule on multiple variables using Apache mod_rewrite

I would like to know how can I rewrite www.site.com?lang=lv&type=1&mode=2 into www.site.com/lv/1/2 using Apache mod_rewrite options.

Upvotes: 2

Views: 4338

Answers (3)

Gumbo
Gumbo

Reputation: 655269

You need to handle the URI path and query separately. If the parameters appear in that very same order, you can do this:

RewriteCond %{QUERY_STRING} ^lang=([^&]+)&type=([^&]+)&mode=([^&]+)$
RewriteRule ^$ /%1/%2/%3? [L,R=301]

Otherwise you will need to put them in the right order before using this rule or take each parameter at a time.

Upvotes: 0

Matt S
Matt S

Reputation: 1757

Basic rewrite:

RewriteEngine On
RewriteRule http://www.site.com/?lang=(.+?)&type=(\d+?)&mode=(\d+?) http://www.site.com/$1/$2/$3 [L,R=permanent]

Edit: Changed rewrite flags to force a permanent redirect

Upvotes: 0

You
You

Reputation: 23774

Assuming that you actually want to rewrite /lv/1/2 to ?lang=lv&type=1&mode=2 (I see no reason to do the opposite) and that no other rewrites are active, this should do the trick:

RewriteRule ^/([^/]*)/([^/]*)/([^/]*)$ ?lang=$1&type=$2&mode=$3 [L]

Also; you'd be better off replacing those magic numbers with more useful information if you want to include them in your URI.

Edit: If it really is the opposite you'd like to do, see the answer by Matt S.

Upvotes: 2

Related Questions