mod_rewrite remove a GET variable

后端 未结 2 1618
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 21:28

I\'m trying to take

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

and redirect to

example.com/fr/home?foo=bar

相关标签:
2条回答
  • 2021-01-22 21:42

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

    0 讨论(0)
  • 2021-01-22 21:56

    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]
    
    0 讨论(0)
提交回复
热议问题