Mike Monteith
Mike Monteith

Reputation: 541

mod_rewrite remove a GET variable

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

Upvotes: 2

Views: 4641

Answers (2)

regilero
regilero

Reputation: 30496

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

Upvotes: 5

whoisgregg
whoisgregg

Reputation: 75

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]

Upvotes: 2

Related Questions