mod_rewrite and .htaccess: redirect based on browser language

The previous version of 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, 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 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 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]

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 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‘…