Jump to content

Apache: Redirect to Language Path by Browser Language Setting

+ 1
  Adam Musial-Bright's Photo
Posted May 05 2011 01:42 AM

Recently I had to find a solution to redirect users to the right language on a multilingual website. I decided to use the browser language setting, which is send with every request you make. The website languages where basically distinguished by an simple /en, /de, /pl prefix, so all I had to do was:

read the browser language
redirect to the coresponding language page (/en, /de, ...)
if there is no browser language/prefix match, redirect to /en as fallback

I foud a good example in this blog post, which I summerised here:

RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^/$ /de/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^/$ /es/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^pl [NC]
RewriteRule ^/$ /pl/ [L,R=301]
RewriteRule ^/$ /en/ [L,R=301]

As you can see in the first line, the apache server uses the rewrite mod. The second and third line you can see as a pair, where RewriteCond is the rewrite condition and the RewriteRule is the rule if the condition matches.
So if the request header parameter Accept-Language starts with de, the respond with moved permanently (301) and the new path /de should be provided.
The last line is the fallback rule tho the english language.

0 Subscribe

0 Replies