Automatically detecting browser language using htaccess

The tourism organization for Lake Lucerne has just launched a great-looking new website. Modern design, nice typography, beautiful pictures and a simple navigation all add to the experience.

But one small improvement could be made by adding a few simple lines of code to the .htaccess file: making sure that visitors to the site automatically land on the version of the website in their language.

At the time of writing this blog post, visiting www.lakelucerne.ch will land you on the German version of the website. If you want to see the site in English, you’ll need to know that the button Deutsch at the top of the page is a language switcher.

However, a quick test indicates that the German home page is actually available at www.lakelucerne.ch/de and that the English home page is available at www.lakelucerne.ch/en. This means that the visitor can be redirected there automatically: detecting the browser language when the root page of the website is requested and redirecting to the start page in the appropriate language.

If there is no version of the website available in the requested language, then a fallback to English is a pretty good idea, as this is the most commonly understood international language.

Better still: the site is powered by TYPO3, which allows, via RealURL, a separate domain per language. So you could redirect German-speaking visitors to a German domain name and keep English-speaking visitors at lakelucerne.ch. That would eliminate the need for /de or /en altogether.