I\'m trying to take
example.com/home?lang=fr&foo=bar
and redirect to
example.com/fr/home?foo=bar
>
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 &
.