Angel Miladinov
Angel Miladinov

Reputation: 1655

Rewrite url with .htaccess based on GET parameters

I want to rewrite the following url pattern:

http://example.com/some/inner/page?param1=test&lang=en

becomes

http://example.com/en/some/inner/page?param1=test

. So basically I want to take the "lang" get parameter and put it at the begginning of url. I know I have to use RewriteRule in the .htaccess but what is the pattern that has to be written ? Note that I also need it to work for both ?lang=en and ?some-parameter=test&lang=en

Upvotes: 1

Views: 48

Answers (1)

anubhava
anubhava

Reputation: 786291

You can use this rule as your top most rule in root .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} \?(.*&)?lang=([^&]+)&?(\S*)\sHTTP [NC]
RewriteRule ^ /%2%{REQUEST_URI}?%1%3 [R=301,NE,L]

This will remove lang= parameter from anywhere in query string and redirect using value of same parameter at the front.

Upvotes: 1

Related Questions