mod_rewrite and .htaccess: redirect based on browser language

The previous version of miczanin.it had just an index.html file in the root folder of the web server, together with few other supporting html files. When someone tried to access https://www.miczanin.it, the index.html page was automatically loaded.

The current configuration is different. There are two distinct WordPress installations, one in subfolder mz_it (in Italian), one in subfolder mz_en (in English). My goal is to automatically redirect https://www.miczanin.it to mz_it or mz_en, according to browser language preferences.

There are many possible solutions, but I think the cleanest one is to use a server-side approach based on .htaccess file and mod_rewrite module. You can find some documentation, a tutorial and a cheat sheet starting from this page: URL Rewriting for Beginners.

This is the .htaccess file in the root folder of miczanin.it web server:

<IfModule mod_rewrite.c>
# Turn on the rewriting engine
RewriteEngine On 
RewriteBase /

# If Accept-Language starts with 'it',
# then redirect (only) main page to mz_it
RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteRule ^/?$ mz_it/ [R,NC,L]

# Otherwise, redirect to mz_en
RewriteRule ^/?$ mz_en/ [R,NC,L]
</IfModule>

The idea is to exploit the optional Accept-Language information that browsers can embed in their HTTP requests. According to the official documentation, it is a comma separated list of language definitions. It could be straightforward like ‘it‘, or ‘en‘, but something more complicated like ‘en-gb;q=0.7‘ is also a valid declaration.

If the language list starts with ‘it‘, then https://www.miczanin.it is redirected to mz_it. If no language preferences are defined, or if Italian is not the first choice, then the redirection goes to mz_en.

In case ‘it‘ appears before ‘en‘, but not in first place (example ‘fr, it, en‘), then the .htaccess rules still redirect to mz_en. This behaviour is not desirable, but for the moment I consider the described solution acceptable. I wonder if it is possible to devise a regular expression for RewriteCond that looks for ‘it‘ appearing before any of the ‘en-*‘ or ‘en‘…

2 thoughts on “mod_rewrite and .htaccess: redirect based on browser language

  1. How to get this done if wordpress is in wp directory ?
    This don’t work

    [code]
    # Turn on the rewriting engine
    RewriteEngine On
    RewriteBase /wp/

    # If Accept-Language starts with ‘it’,
    # then redirect (only) main page to mz_it
    RewriteCond %{HTTP:Accept-Language} ^sl [NC]
    RewriteRule ^/wp/?$ / [R,NC,L]

    # Otherwise, redirect to mz_en
    RewriteRule ^/wp/?$ /en/ [R,NC,L]
    [/code]

Leave a Reply to Luka Cancel reply

Your email address will not be published. Required fields are marked *