Redirect according to browser language: mod rewrite and HTTP_ACCEPT_LANGUAGE

.htaccess, apache, mod-rewrite Leave comment

I wanted to redirect users to the main page of the website according to his/her browser default language setting.

This setting is sent by the browser at every request to the server as ‘Accept-Language’ header and can look like this “pl,en-us;q=0.7,en;q=0.3″. You can check it with Live HTTP Headers Add-on for Firefox and read more about this header at W3C HTTP specification.

In PHP I could do it by checking the value of $_SERVER['HTTP_ACCEPT_LANGUAGE'] and issuing the header(”Location: /LANG/foo.php”,TRUE,301); command.

But I didn’t want to use PHP and did want to do it with .htaccess and mod rewrite. The first thought was to use RewriteCond %{HTTP_ACCEPT_LANGUAGE} as the analogy to RewriteCond %{HTTP_USER_AGENT} or RewriteCond %{HTTP_REFERER}. Unfortunately RewriteCond %{HTTP_ACCEPT_LANGUAGE} does not work! This variable is not present.

What is the solution? Looking more closely at the documentation at we have:

%{HTTP:header}, where header can be any HTTP MIME-header name, can always be used to obtain the value of a header sent in the HTTP request.

Which means we can obtain the value of default language setting of the browser by using RewriteCond %{HTTP:Accept-Language}. This is the simple solution, difficult to find on the web.

So if we have a website with three languages: English, German and Spanish and we want English to be the default we would use for example this in .htaccess:

#The 'Accept-Language' header starts with 'de'
#and the test is case-insensitive ([NC])
RewriteCond %{HTTP:Accept-Language} ^de [NC]
#Redirect user to /de/hauptseite address
#sending 301 (Moved Permanently) HTTP status code
RewriteRule ^$ /de/hauptseite [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^$ /es/pagina-primera [L,R=301]

#For every other language (including English :)) use English
RewriteRule ^$ /en/main-page [L,R=301]

If you need to be more specific (eg. redirect accordingly to US or Great Britain English (en-us and en-gb)) you would not check only the first two signs of ‘Accept-Language’ header but you would need to prepare a little bit more complicated regular expression (some language settings are only two signs, some five signs).

I also thought about possibility of changing redirection addresses from within the CMS system (so that the admin can change main-page, hauptseite or pagina-primera to whatever he/she wishes). The best way would be to save this information in another file (e.g. lang-redirects.conf) and include it within .htaccess. Unfortunately config files inclusion is not possible in .htaccess scope. Another solution I thought about was using mapping-function but it doesn’t work, either.
So the only we can do is to edit .htaccess itself. If anyone knows a nicer solution  please share it with us.

Do you know any other tips & tricks regarding .htaccess and/or mod rewrite? We are waiting.

  • Share/Bookmark

15 Responses to “Redirect according to browser language: mod rewrite and HTTP_ACCEPT_LANGUAGE”

  1. AdamNo Gravatar Says:

    Thanks for the post…
    I tested the headers with send http tool and its working fine.

  2. adamNo Gravatar Says:

    sorry, the link to the tool is:

  3. Adam JakabNo Gravatar Says:


    Cheers, this is exactly what i was looking for but for me it needs some modding for simplicity. That is to say: given that i have a site with 11 languages (and it is still to grow) and in each language the home page is:

    i’d like to do something similar:

    #if you have requested
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule (.*) /{HTTP:Accept-Language}/home.html [R=301,L]

    Obviously, the “Accept-Language” string contains stuff like “en-us,en;q=0.5″ so the rule above would NOT work like this.

    Do you know a way of how to extract only the first 2 chars and use that for the rewrite part?


  4. AlexNo Gravatar Says:

    Nice one! Thanks!

  5. zac cravenNo Gravatar Says:

    Doesn’t this code make it impossible for an english browser to view the spanish site?

    In my case, I want to redirect the user only when they visit the root page (, to the correct language version (e.g. If they visit any non-root page (e.g. “”) then I do not want to redirect them anywhere.

  6. MattNo Gravatar Says:

    I’ve run into a problem with this code. If an non-English speaker wants to visit the English site, it won’t let them see it. It redirects them back to their native language. Any idea how to change this?

  7. AjiNo Gravatar Says:

    Worked fine!

  8. MattNo Gravatar Says:

    I found the fix, btw. You simply cannot put your main landing homepage/files in the root. They should be in a subdirectory such as /en/ or /es/

  9. spywareremovalhelpNo Gravatar Says:

    Thanks for your good job…
    I tested the headers with my site: remove fake windows restore virus ,the htacess RewriteRule worked fine!

  10. Web dizajnNo Gravatar Says:

    Working! Thanks!

  11. EasyNo Gravatar Says:

    Nice idea, but why exactly are you using permamnet redirects?!

  12. MikeNo Gravatar Says:

    “Nice idea, but why exactly are you using permamnet redirects?!”

    I would be interested to that reason too.

  13. AdamNo Gravatar Says:

    Thanks, worked well!

  14. MauroNo Gravatar Says:

    I need to write a conditional 301 Nginx rewrite rule based on browser language

    This rules were working fine with Apache/htaccess:

    RewriteCond %{HTTP_HOST} ^(www\.)?profumo\.it$
    RewriteCond %{REQUEST_URI} ^blog/$
    RewriteCond %{HTTP:Accept-Language} ^it [NC]
    RewriteRule ^blog/$ MY-ITALIAN-URL? [NC,L,R=301]

    RewriteCond %{HTTP_HOST} ^(www\.)?profumo\.it$
    RewriteCond %{REQUEST_URI} ^blog/$
    RewriteCond %{HTTP:Accept-Language} !^it [NC]
    RewriteRule ^blog/$ MY-ENGLISH-URL? [NC,L,R=301]
    That is:


    you browser language is Italian redirect to the page “MY-ITALIAN-URL”


    redirect to the page “MY-ENGLISH-URL”

    I am new with Nginx and I couldn’t find examples or tutorial on that.

    Can anyone convert it in Nginx syntax please?

    Thank you Mauro

  15. Acton coleNo Gravatar Says:

    I would like to see the profile

Leave a Reply

Verification Image

Please type the letters you see in the picture.

This site is using OpenAvatar based on

Comment Spam Protection by WP-SpamFree

Engine: Wordpress.
Log in