Question

I need to redirect my users in homepage base on their language. I think I've to use RewriteCond %{HTTP:Accept-Language} but I can't figure out. My last try:

RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule .* http://mydomain.com [R,L]


RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule .* http://en.mydomain.com [R,L]

RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule .* http://fr.mydomain.com [R,L]

But this generate an infinite loop.

Folder structure

  • htdocs/index.php ->mydomain.com
  • htdocs/en/index.php ->en.mydomain.com
  • htdocs/fr/index.php -> fr.mydomain.com
Was it helpful?

Solution

You need to make sure the HOST isn't already pointing to the correct place:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^mydomain.com$ [NC]
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule .* http://mydomain.com [R,L]

RewriteCond %{HTTP_HOST} !^en.mydomain.com$ [NC]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule .* http://en.mydomain.com [R,L]

RewriteCond %{HTTP_HOST} !^fr.mydomain.com$ [NC]
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule .* http://fr.mydomain.com [R,L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top