Rewrite rule on multiple variables using Apache mo

2019-07-16 04:03发布

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.

3条回答
够拽才男人
2楼-- · 2019-07-16 04:24

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

查看更多
迷人小祖宗
3楼-- · 2019-07-16 04:46

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.

查看更多
趁早两清
4楼-- · 2019-07-16 04:46

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.

查看更多
登录 后发表回答