Wizard
Wizard

Reputation: 11265

.htaccess RewriteRule country codes as a get param

What I want to achieve:

http://example.com/en -> http://example.com/?lang=en

http://example.com/en/something -> http://example.com/something/?lang=en

http://example.com/ru -> http://example.com/?lang=ru

http://example.com/ru/something -> http://example.com/something/?lang=ru

I try with:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.+)$ /?p=$1&lang=en [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ru/(.+)$ /?p=$1&lang=ru [L,QSA]

Upvotes: 1

Views: 60

Answers (1)

arco444
arco444

Reputation: 22821

You can do it like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(ru|en)/(.+)$ /$2/?lang=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(ru|en)/?$ /?lang=$1 [L,QSA]

(ru|en) will match either ru or en. If you wanted to make it a generic 2 letter match, you could use [a-z]{2}

Upvotes: 1

Related Questions