开发者

Rewrite rule on multiple variables using Apache mod_rewrite

开发者 https://www.devze.com 2023-01-05 23:44 出处:网络
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开发者_高级运维.Assuming that you actually want to rewrite /l

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开发者_高级运维.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消