使用 Apache mod_rewrite 重写多个变量的规则

发布于 2024-09-08 17:28:58 字数 139 浏览 0 评论 0原文

我想知道如何将 www.site.com?lang=lv&type=1&mode=2 重写为 www.site.com/lv/1/2代码> 使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痞味浪人 2024-09-15 17:28:58

假设您确实想将 /lv/1/2 重写为 ?lang=lv&type=1&mode=2 (我认为没有理由做相反的事情)并且没有其他重写处于活动状态,这应该可以解决问题:

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

此外;如果您想将这些幻数包含在您的 URI 中,您最好将它们替换为更有用的信息。

编辑:如果这确实与您想要的相反,请参阅 Matt S 的回答。

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.

丢了幸福的猪 2024-09-15 17:28:58

基本重写:

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

编辑:更改重写标志以强制永久重定向

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

你与昨日 2024-09-15 17:28:58

您需要分别处理 URI 路径和查询。如果参数以完全相同的顺序出现,您可以执行以下操作:

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

否则,您需要在使用此规则之前将它们按正确的顺序放置,或者一次获取每个参数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文