mod_rewrite的删除一个GET变量 [英] mod_rewrite remove a GET variable

查看:85
本文介绍了mod_rewrite的删除一个GET变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图采取

example.com/home?lang=fr&foo=bar

example.com/home?lang=fr&foo=bar

和重定向到

example.com/fr/home?foo=bar

example.com/fr/home?foo=bar

我试过

    RewriteCond %{QUERY_STRING} lang=([a-z]{2})&?
    RewriteRule ^(.*) /%1/$1 [R=307]

但我得到一个重定向循环。
这是因为LANG = FR从FR后,查询字符串被移到请求字符串前面没有带走。其结果是:

but I get a redirect loop. This is because the lang=fr is not taken away from the query string after 'fr' is moved to the front of the request string. This results in:

example.com/fr/fr/fr/fr/fr/fr/fr/fr/fr /....

example.com/fr/fr/fr/fr/fr/fr/fr/fr/fr/....

我可以使用

    RewriteRule ^(.*) /%1/$1? [R=307]

但也将删除查询字符串的富=酒吧,我想保持完好

but that also removes the foo=bar from the query string which I want to keep intact

我无法找到查询字符串中移除只是一个变量的方法。我敢肯定,有一个简单的解决方案,但谷歌并没有帮助。
谢谢

I cannot find a way of removing just one variable from the query string. I'm sure there is a simple solution but google hasn't helped. Thanks

推荐答案

在补充@whoisgregg答案(+1),这是从维基阿帕奇一个很好的适应性。规则有没有complelty尼斯(例如,通过此页面上查询字符串中的访问控制would'nt抵御有点URL编码)。

In complement to @whoisgregg answer (+1), which is a nice adaptation from the apache wiki. Rules there are not complelty nice (for example the access control by query string on this page would'nt resist on a little urlencoding).

我们有测试用例工作解决方案,但一些扩展测试可能会失败:

We have a working solution for the test case, but some extended tests may fail:

RewriteCond %{QUERY_STRING} ^(.*)lang=([a-z]{2})&?(.*)$
RewriteRule (.*) /%2/$1?%1%3 [R=307]

foo.php?foo=bar&novlang=fr&toto=titi => /fr/foo.php?foo=bar&novtoto=titi

下面的 LANG = 检测一部分,但如果任何一个参数结尾的的你就会有问题。

Here the lang= part is detected but if any parameter ends with lang you'll have a problem.

所以,我们需要一些修复,我一直在这个星期的工作在这个问题上,这里是一个更好的版本,我认为:

So, we need some fix, I've been working on that subject this week and here's a better version I think:

RewriteCond %{QUERY_STRING} (.*)(^|&|%26|%20)lang(=|%3D)([^&]+)(.*)$
RewriteRule (.*) /%4/$1?%1%5 [R=307]

foo.php?foo=bar&novlang=fr&toto=titi => not matched (normal, no lang=)
foo.php?foo=bar&lang=fr&toto=titi&b=a => fr/foo.php?foo=bar&toto=titi&a=b

在这里挂起的唯一的问题是,可以部分或完全地URL连接codeD,也不会被检测到,但是这是一个罕见的病例。我ALOS取消了对郎值仅有2个字符的控制。我觉得([^&安培] +)是更好,这意味着所有的字符,直到我匹配&安培;

The only problem pending here is that lang could be partially or completly url encoded and would not be detected, but that's a rare case. I alos removed the control of having only 2 chars on the lang value. I think ([^&]+) is better, it means all characters until I match an &.

这篇关于mod_rewrite的删除一个GET变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆