Join over 10,000 ecommerce enthusiasts who receive free updates
Get email updates
Build A "Smarter"
Magento Store
Magik Extra Fees Create unlimited number & types of extra fees and charges to drive increased revenue per order.
$199  $99

Most popular posts

Browser Language Redirection For Multi Language Stores In Magento

 

Do you run a Multilingual Magento store and looking to auto redirect your customers to their language store? Well, browser language redirection is what we are talking about which can run on Firefox, IE, Mozilla, Google Chrome, Avant and Safari browsers. Generally, Magento does not support geographical detection and redirection so many store owners are initially getting customers to land on their default page where they ask them to choose their preferred language. This is the default way of redirecting customers to their local language store. Other option is to choose a country drop-down or flag to redirect customers to their language store (widely used in multi language stores).

What about detecting the customers default language automatically and open specific language store without having those to select their country/language choice manually?

Automatic redirection based on Customers Location, Geographical detection and browser language.

Many people debate that Google or other search engine robots do not wish to see automatic redirection and you can be penalized for doing this. In my opinion it is true but there are some definite workarounds which can solve your problem without being penalized by search engines. You can specifically tell search engines and show them links which are necessary for SEO after redirection or may be before redirection. A well devised strategy can help in SEO and avoid penalties. If you are choosing this type of redirection make sure that you follow Google’s guidelines related to redirection and SEO, some best practices can further help.

Let’s take an example where you have a Magento multi language store (http://www.magento-exmaple-store.com) already setup:

  1. Default store language is English
  2. Multi language stores are in French and Spanish.

Customers coming from Spain or customers coming from France should redirect to http://www.magento-exmaple-store.com/es or http://www.magento-exmaple-store.com/fr respectively. I am assuming that you have already assigned/enabled store codes in URL’s via Admin Panel => System => Configuration => Web => Url Options => Add store code to Urls=> Yes

This ensures that any customers coming from the above mentioned geographic area should be redirected to their browser language store. In our case (French {fr} or Spain {es}) otherwise they will land on default English version of your store. Check Valid country code list for reference.

Prerequisites

  1. Server should be mod_rewrite = enabled.
  2. Store codes are enabled via admin as mentioned above.
  3. URL Rewriting is enabled (Admin Panel => System => Configuration => Web => Url Options => Use Web Server Rewrites => Yes)
  4. Two letter browser local code created (e.g. fr for French and es for Spanish) Admin Panel => System => Manage Stores
  5. Default store (English) should have store code “en” and sort order “0”.
  6. Spanish store “es” created with sort order “1” & French store code “fr” with sort order “2”.
  7. All these three stores are accessible via browser i.e. http://www.magento-exmaple-store.com http://www.magento-exmaple-store.com/es and http://www.magento-exmaple-store.com/fr


Auto Redirection To Browser Language Store

In order to automatically redirect the customers to their browser language settings we need to edit index.php file which will be found in your store’s root directory i.e. http://www.magento-exmaple-store.com/index.php

Edit the file and place the following piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* Check For Browser languge to redirect to appropriate store */
function checkStoreLanguage()
{
   if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
       foreach (explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])) as $accept) {
           if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!", trim($accept), $found)) {
               $langs[] = $found[1];
               $quality[] = (isset($found[3]) ? (float) $found[3] : 1.0);
           }
       }
       // Order the codes by quality
       array_multisort($quality, SORT_NUMERIC, SORT_DESC, $langs);
       // get list of stores and use the store code for the key
       $stores = Mage::app()->getStores(false, true);
       // iterate through languages found in the accept-language header
       foreach ($langs as $lang) {
           $lang = substr($lang,0,2);
           if (isset($stores[$lang]) && $stores[$lang]->getIsActive()) return $stores[$lang];
       }
   }
   return Mage::app()->getStore();
}
 
/* Automatically redirect to language fr or es store view if request is for root */
if ($_SERVER['REQUEST_URI'] === '/') {
   header('Location: '.checkStoreLanguage()->getBaseUrl());
   exit;
}
 
#Varien_Profiler::enable();

#Mage::setIsDeveloperMode(true);

#ini_set('display_errors', 1);

umask(0);
Mage::run();

The above code will first parse the HTTP_ACCEPT_LANGUAGE header sent by browser with priority code and desired language code. [Ref: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4]. Your sort order will tell Magento which is the most preferred store (in our case “0” which is English version of the store). It will loop through the entire store views and look for ISO 639-2 letter country code (in our case en, fr and es), finally it will return whether the country code is found otherwise opens default store view which is English version of the store.

1
2
3
4
if ($_SERVER['REQUEST_URI'] === '/') {
   header('Location: '.checkStoreLanguage()->getBaseUrl());
   exit;
}

Function call to checkStoreLanguage will ensure that when root “/” (if Magento is installed in other directory than root change it accordingly) is called it fetches the correct store view and redirect the customers to their preferred language store view.


How To Test Automatic Redirection

There are two methods which can help you to test the entire redirection process:

  1. Use Firefox plug-in called Quick Locale which is by far the easiest language switcher.
  2. You can optionally change your browser’s language setting. Read the instructions carefully on how to change your browser language settings.


Browser Language Redirection on IP Based Geolocation

There are various third-party tools/APIs available i.e. http://www.ipligence.com/geolocation/ which can be used to detect the customers IP address by sending ($_SERVER['REMOTE_ADDR']). Based on the return value you can redirect your users to a particular store view.

I hope this will help the browser language redirection and gain more trust from a customer’s point of view. Now, your customers don’t have to choose their language prior to their purchase but they will be presented with their very own language site. I would love to hear your experiences on the same. Please leave me a comment and let me know. Subscribe our RSS to receive latest Magento updates, tips and tricks.

 
Enjoyed this Post?
Then you can follow us here:

Subscribe to RSS
Follow us on Twitter
Follow us on Facebook

Ashish Nayyar

Chief Product Officer & Architect. MagikCommerce.com

Build A "Smarter" Magento Store
Magik Extra Fees Magik Extra Fees is the #1 extention for creating unlimited number & types of extra fees and charges to drive increased revenue per order.
  • Extra Fee for Products
  • Extra Fee for Categories
  • Extra Fee for Shipping
  • Multiple Additional Charges
$199  $99
 
  • Kainer

    Hi there,

    thanks a lot :)
    That was exactly the thing i was looking for :)

    And it works great so far. But one question still remains:

    I have 2 StoreViews (en/de (ger)); not as in your example three. The default StoreView is german (Count 0)
    So now if someone from France enters my site, he should be redirected to the german StoreView. But he ends on the english one, whats even better, but i dont know why.

    I tried to edit your code to:
    $lang = substr($lang,0,1);
    but then redirecting ends on my german Storeview, even if my browser “speaks” english.

    Any tipps?

    Thanks and Greetings,

    Kaines

  • http://www.onlinevoucher.net/vouchers.php Chance Draft

    I realy liked this innovative angle that you have on the topic. Certainly wasn’t planning on this at the time I begun searching for tips. Your ideas were totally simple to get. Im glad to find that there’s an individual here that gets it precise what its is talking about.

  • jbg

    It seems it’s not working on 1.5, this generate error on back and front end, I set fr as prefered language and en as second option, if you have any idea

  • Tanya

    Where I need to put that code in index.php? when i put that code, it give me a error in front or back, like @e8f5bcb71608800ee79b8167a135928a:disqus .
    How can i do this? i need it!

    thanks!

  • Sander Van Zuidam

    Indeed doesn’t work with 1.5 anybody found a solution?

  • Aleem Ahmad

    It works on local machine but not on the live server. all the browsers ive the same error.

    “Firefox has detected that the server is redirecting the request for this address in a way that will never complete.”

  • Aleem

    It works great. was my mistake. i was not following the points.

  • Rana

     I found good example and worked for me, quick and easy steps.  

    http://www.phptechi.com/magento-show-country-flags-selector-in-header.html

  • john

    it is possible to redirect customer without turn on store code urls?

  • Tanuj

    When I enables Use Web Server Rewrites, then I am unable to login from front-end.

  • Qaiser

    you made a nice sharing. i have used it in my store, which is basically a multi-store. in one of the stores, i have an extension which redirects user to login page i.e. user can not enter store unless he logs in.
    I basically have problem with this login compulsory store. this code is not working here. can u give any suggestion plz.

  • Underskyish

     didn’t work here