.htaccess language redirects with SEO-friendly URLs

31,224

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:
31,224

Related videos on Youtube

Zistoloen
Author by

Zistoloen

I like Stack Exchange's websites network because it's so well designed and SEO-optimized. Therefore, I spend time to the community to help people on webmastering issues, to improve formatting of many posts to keep this website clean, etc. I am the first Steward and Marshal of Pro Webmasters :-). Otherwise, I love making websites with WordPress, HTML5, CSS3, JavaScript, jQuery... SEO is also a good stuff. I support these two projects: GitStack (git server for Windows) and CamSwipe (credit card OCR for mobile). My profile on Stack Exchange's network:

Updated on September 18, 2022

Comments

  • Zistoloen
    Zistoloen over 1 year

    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]
    
    • Admin
      Admin over 10 years
      Whats the output of RewriteLog ?
    • hjpotter92
      hjpotter92 over 10 years
      I think the question is more appropriate for Webmasters.
    • Admin
      Admin over 10 years
      @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"
    • Admin
      Admin over 10 years
      @hjpotter92 Can I just move this question to Webmasters?
    • hjpotter92
      hjpotter92 over 10 years
      Click the flag link under the question, chose the "Other" option. State that the post be move to Webmasters.
  • dan
    dan over 10 years
    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.
  • eapo
    eapo about 8 years
    "This webpage has a redirect loop" for me. with a simple index.php (has only html content), nothing else in .htaccess. What can be the problem?