Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

How do I setup my .htaccess file to detect several languages, and redirect them to specific SEO-friendly URLs?

Basically every URL needs to go to index.php?lang=(...).

So, for English language detection http://mysite.com has to go to http://mysite.com/en/ (index.php?lang=en).

My .htaccess as of now (not working):

RewriteEngine On

RewriteCond %{HTTP:HOST} http://mysite.com/

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

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

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

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

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

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

RewriteRule ^(en|de|nl|fr|es)$ index.php?lang=$1 [L,QSA]
share|improve this question
    
Whats the output of RewriteLog ? –  DanFromGermany Oct 7 '13 at 11:13
    
I think the question is more appropriate for Webmasters. –  hjpotter92 Oct 7 '13 at 11:24
    
@DanFromGermany I can't seem to get the RewriteLog working. apache_error.log says /Applications/MAMP/htdocs/mysite/.htaccess: RewriteLog not allowed here, even though I specified RewriteLog "htaccesslogs.txt" –  jlmmns Oct 7 '13 at 11:28
    
@hjpotter92 Can I just move this question to Webmasters? –  jlmmns Oct 7 '13 at 11:29
    
Click the flag link under the question, chose the "Other" option. State that the post be move to Webmasters. –  hjpotter92 Oct 7 '13 at 12:29
add comment

migrated from stackoverflow.com Oct 21 '13 at 21:07

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 4 down vote accepted

You're almost there. Drop the very first RewriteCond (as it's not required and won't match ever) and add a trailing / to your last RewriteRule since you're redirecting with them.

RewriteEngine On

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

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

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

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

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

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

RewriteRule ^(en|de|nl|fr|es)/?$ index.php?lang=$1 [QSA,NC,L]
share|improve this answer
    
Thanks, this is working nicely! –  jlmmns Oct 7 '13 at 12:52
add comment

Here is my code for language and country parameters. This supports any language:

#www.country.com/index.html  becomes www.country.com/index.php
#www.country.com/fr/index.html  becomes www.country.com/index.php?language=fr
#www.country.com/ca/fr/index.html  becomes www.country.com/index.php?country=ca&language=fr

RewriteRule ^([a-zA-Z]{2})/([a-zA-Z]{2,3})/index\.html$ index.html?country=$1&language=$2
RewriteRule ^([a-zA-Z]{2,3})/index\.html$ index.html?language=$1
RewriteRule ^index\.html$ /index.php [L]
share|improve this answer
    
Welcome to the site. Answers here are required to be in English only. Please see the edits I made for you here to help with your answers in the future. –  dan Jan 27 at 22:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.