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

Renovation

The first version of miczanin.it website was published in 2004. It was just a single html page with a list of links to various distinct subsections (curriculum vitae, family tree, photogallery, etc.).

Main page of miczanin.it just before renovation.

Main page of miczanin.it just before renovation.

Now it is time to renovate it. I am using mainly WordPress technology, with two distinct installations, one for the Italian version, one for the English. Static pages will be approximately the same, while blog sections will be completely independent. This one (in English) will deal mainly with work related topics.