mod_rewrite remove a GET variable

偶尔善良 提交于 2019-12-02 03:54:49

问题


I'm trying to take

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

and redirect to

example.com/fr/home?foo=bar

I tried

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

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/....

I can use

    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


回答1:


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

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

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 &.




回答2:


This is a variation of a technique from the apache wiki that should do the trick:

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


来源:https://stackoverflow.com/questions/7756688/mod-rewrite-remove-a-get-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!