errorous
errorous

Reputation: 1071

.htaccess for redirecting languages in php app

I have a multingual website. In PHP, I'm making all the pages have unique id, so that I know which files to include. All the logic is going through index.php page.

So, I'd like to have montkuce.com/en/foobar to send a request to index.php via GET like this: index.php?id=foobar&lang=en

This is what I have so far, and it doesn't work:

RewriteEngine On

RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ http://montkuce.com/en/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ http://montkuce.com/de/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ http://montkuce.com/fr/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteRule ^$ http://montkuce.com/it/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^cz [NC]
RewriteRule ^$ http://montkuce.com/cz/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^sk [NC]
RewriteRule ^$ http://montkuce.com/sk/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(en|de|fr|it|cz|sk)/(.*)$/?$ index.php?lang=$1&id=2 [QSA,NC,L]

Upvotes: 1

Views: 227

Answers (1)

anubhava
anubhava

Reputation: 785156

Your regex is little off in the last rule. Use this:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|de|fr|it|cz|sk)/(.*?)/?$ /index.php?lang=$1&id=2 [QSA,NC,L]

Upvotes: 2

Related Questions