How to set up redirect by language in htaccess

This article explains how to configure an Apache web server serving multilingual content to automatically redirect users to their language section according to preferrable language selected in their browser.

There is an easy way not requiring programming – just copy a few lines into the .htaccess file. Below are code examples for different languages.

Why to use the redirect based on the browser language

When creating a multilingual website, there are different approaches to how users will choose the language of content.

Automatic language detection by IP address

This approach is based on the fact that each IP address corresponds to a specific country or region. When the server detects that the user is connected for example from Germany, the site could lead the user to German section.

There are some questions with this:

  • if that’s a country where people speak different languages, like Belgium or Switzerland?
  • if the user is currently connected not from his original country (being on a business trip or on vacation), for example, a Frenchman having rest in Thailand?

And this option will require programming for example in PHP to link IP addresses with languages.

Language selection by the user and saving in cookies

The approach is firstly to show a page in English where users can select a preferred language. Then the server saves user selection is cookies and use saved preferences each time that user requests a page. The downside is that some users will immediately close the page in a language they do not understand without choosing preferred language.

And this requires programming too.

Automatic language detection based on browser settings

Language settings of client’s web browser is available to the website. The browser language on the user’s device does not change when the user moves between regions or countries, so even if he or she is connected from another country, the site will still be using his or her language.

Therefore, the user will immediately receive content in a language he understands. And this approach does not require programming.

So this way solves the language task and it is easy to implement.

How to set up language based redirect in htaccess

Copy the conditions and rules of the redirect to the ReWrite section, for example, these lines check that German is the preferred language and, accordingly, redirect the user’s browser to the /de/ section:

RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ http://%{HTTP_HOST}/de/ [L,R]

For more details, see the documentation links at the end of the article.

Same way you can add more languages, and at the end specify the default language. For example more complete set up for Rewrite module in the .htaccess file could be written like this:

RewriteEngine On
# Danish
RewriteCond %{HTTP:Accept-Language} ^da [NC]
RewriteRule ^$ http://%{HTTP_HOST}/da/ [L,R]
# German
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ http://%{HTTP_HOST}/de/ [L,R]
# Spanish
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^$ http://%{HTTP_HOST}/es/ [L,R]
# Finnish
RewriteCond %{HTTP:Accept-Language} ^fi [NC]
RewriteRule ^$ http://%{HTTP_HOST}/fi/ [L,R]
# French
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ http://%{HTTP_HOST}/fr/ [L,R]
# Italian
RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteRule ^$ http://%{HTTP_HOST}/it/ [L,R]
# Dutch
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteRule ^$ http://%{HTTP_HOST}/nl/ [L,R]
# Norwegian
RewriteCond %{HTTP:Accept-Language} ^no [NC]
RewriteRule ^$ http://%{HTTP_HOST}/no/ [L,R]
# Portuguese
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^$ http://%{HTTP_HOST}/pt/ [L,R]
# Swedish
RewriteCond %{HTTP:Accept-Language} ^sv [NC]
RewriteRule ^$ http://%{HTTP_HOST}/sv/ [L,R]
# English as default
RewriteRule ^$ http://%{HTTP_HOST}/en/ [L,R]

As always, it’s a good idea to back up the file when making changes. Just in case.

Modified .htaccess file should be uploaded to root folder of the web site using any FTP/SFTP client software lile FileZilla or PSFTP.

How to check htaccess settings

It is recommended to check the modified .htaccess file before uploading to the production server.

The file can be verified for errors with an online service like http://htaccesscheck.com/.

To test how language based redirection works, you need to open a website in the browser (its home page), each time with a different language in the settings, and the browser should automatically go to the language section.

How to add even more languages

For each language, you need to insert two lines in .htaccess – one line with the RewriteCond condition, the second line with the RewriteRule redirect, similar to the one shown in the code snippet above.

Each language has a two-letter abbreviation according to ISO 639-1. Please note this designation does not match the two-letter country designation, for example, for Danish, Swedish, and many others. See the link at the end of the article for more details.

Summary

WIth ReWrite module settings in the .htaccess file you can easily in a few minutes without programming by simply copying lines to implement automatic redirection to the language selected in the user’s browser.

Know more

Apache Module mod_rewrite

Apache: Content Negotiation

Language codes


Comments

Leave a Reply

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