4.3
(21)

Last Updated on November 30, 2021 by Amit

Redirect users based on browser language

htaccess redirect visitors based on browser language

If you have a multilingual website that is available to users in the language that they understand, you can set an automatic redirect with htaccess that will redirect your users to the language version their browser is in.

For example a user visiting your site from Spain if their browser language is “es” then RewriteRule can automatically redirect the user to “es” version of your website.
With RewriteRule this can be done by checking the %{HTTP:Accept-Language} variable. This variable automatically defects browser default language.


%{HTTP:Accept-Language} is a pre-defined mod-rewite server variable and used for detecting browser language in mod-rewite.
The following is a basic example of browser language detection. The rule will redirect your website hompage to example.com/en if the rule’s condition is met.

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

This RewriteRule will redirect / the hompage of your website to example.com/en if the RewriteCondition RewriteCond %{HTTP:Accept-Language} en [NC] is met. The condition checks the browser language and if it finds en in the language string then the rule is applied.
This rule will not get applied to browsers with language other then “en”.

Detect multiple browser languages with RewriteRule

To detuct multiple languages and redirect based on the deducted language string you can use the following:

RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (en|es|fr) [NC]
RewriteRule ^/?$ http://example.com/%1 [L,R]

The above rule works for 3 languages “en ,es and fr” . The rule automatically defects browser languages and redirects the hompage to example.com with the available lang path.

How useful was this post?

Click on a star to rate it!

Average rating 4.3 / 5. Vote count: 21

No votes so far! Be the first to rate this post.