How to Redirect Based on Browser Language in Apache


apache redirect based on browser language

If your website attracts a global audience, then you can redirect users to language-specific web pages, based on their browser language. This will provide a more personalized user experience. Let us take a look at how to redirect based on browser language in Apache.

 

How to Redirect Based on Browser Language in Apache

Here are the steps to redirect based on browser language in Apache.

Before you proceed, please ensure that you have enabled .htaccess (mod_rewrite) in Apache. If not, then here’s how you can do it:

 

Place your .htaccess file in the root document folder of your website (/var/www/html)

 

1. Open .htaccess file

Open a terminal session or SSH into your Linux system. Open .htaccess file in a text editor


$ sudo vim /var/www/html/.htaccess

 

2. Redirect based on browser language

Here’s a simple tool that you can use to generate the rewrite rules for your language. Use the tool to generate RewriteRule and paste them in .htaccess file. You just need to enter the URL that you want to redirect, and select language.

For example, let’s say you have different websites for French, English and Deutsch languages, then you can add the following rules to your .htaccess file to redirect visitors based on their browser language.

RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (fr) [NC]
RewriteRule .* /fr/index.html [L]
RewriteCond %{HTTP:Accept-Language} (en) [NC]
RewriteRule .* /en/index.html [L]
RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteRule .* /de/index.html [L]

 

 

 

3. Restart Apache web server

Restart Apache web server to apply changes


$ sudo /etc/init.d/apache2 start [Debian or Ubuntu]
# sudo apachectl restart [RHEL, CentOS or Fedora]

 

That’s it! Now Apache will automatically redirect website visitors based on their language.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!