2

I have a multi language site (using Transcribe addon) and I would like my users to be redirected to the correct language based on their IP address.

My goal is that when a user visits the root homepage http://www.example.com it will redirect them to http://www.example.com/en if their IP address is from the UK or http://www.example.com/fr is they are from France for example. But once they are on a language they are not redirected. I’ve tried the native IP nation addon but it didn’t seem to work very well so I’ve used the IP Geo addon at http://devot-ee.com/add-ons/ipgeo. But I’m having loads of problems with rewriting the homepage with htaccess, forcing the correct redirect with Transcribe, template parsing order etc… So I’m thinking of starting from fresh if anyone can give me suggestions on how best to do this.

2 Answers 2

1

Doing a similar thing for a site now and here's what we're doing:

  1. Enable the IP to Nation module
  2. Install and activate the Clive Zagno ipgeo plugin
  3. Add code below to EE - we put it in a snippet snp_html-header
  4. Set Transcribe to always use a language abbreviation in the url

{if "{transcribe:language_abbreviation}" == "es"}
  {if "{exp:ipgeo:code}" != "ES"}
    {redirect="index.php?ACT=23&lang=en" status_code="301"}
  {/if}
{if:elseif "{transcribe:language_abbreviation}" == "de"}
  {if "{exp:ipgeo:code}" != "AT" && "{exp:ipgeo:code}" != "DE" && "{exp:ipgeo:code}" != "CH"}
    {redirect="index.php?ACT=23&lang=en" status_code="301"}
  {/if}
{if:elseif "{transcribe:language_abbreviation}" == "fr"}
  {if "{exp:ipgeo:code}" != "BE" && "{exp:ipgeo:code}" != "FR"}
    {redirect="index.php?ACT=23&lang=en" status_code="301"}
  {/if}
{if:elseif "{transcribe:language_abbreviation}" == "it"}
  {if "{exp:ipgeo:code}" != "IT"}
    {redirect="index.php?ACT=23&lang=en" status_code="301"}
  {/if}
{if:elseif "{transcribe:language_abbreviation}" == "nl"}
  {if "{exp:ipgeo:code}" != "BE" && "{exp:ipgeo:code}" != "NL"}
    {redirect="index.php?ACT=23&lang=en" status_code="301"}
  {/if}
{/if} 

I hope this helps you or someone else.

Edit:

Expanding on this, if you have root access you can install the php5-ipgeo module and add a global variable. That way you don't need the extra two EE addons.

In index.php:

if (function_exists('geoip_country_code_by_name')) {
    $assign_to_config['global_vars'] = array('global_country-code' => geoip_country_code_by_name($_SERVER['REMOTE_ADDR']));
}
3
  • Paul, I'm interested in your comment: "Expanding on this, if you have root access you can install the php5-ipgeo module and add a global variable. That way you don't need the extra two EE addons." Could you provide more details, please? I added the code that you mentioned but I believe that in root should be geoip files as well. Can you provide more detailed instruction how to do it? Thanks in advance!
    – Bosmi
    Oct 26, 2016 at 10:36
  • Hi Bosmi. The instructions for installing the GeoIP PHP Extension differs depending on your webserver set up. Basically here's what you want to install: php.net/manual/en/geoip.setup.php Try searching for instructions specific to your server or ask your hosting company. Once correctly installed and enabled it would appear when using phpinfo();
    – Paul
    Nov 16, 2016 at 21:00
  • @Bosmi btw, by ROOT access I don't mean so that you can upload files to your document root, but rather that you'd need access to the server root in order to install software.
    – Paul
    Nov 16, 2016 at 21:04
1

I gave up with doing this with the Transcribe addon and did it client side instead....

// if at root homepage
if (/^http(s?):\/\/[^\/]*\/?$/.test(window.location.href)) {
    var url = "http://api.wipmania.com/jsonp?callback=?";
    $.getJSON(url, function(data) {
        switch (data.address.country_code) {
        case 'GB':
            location.href = location.href + "index.php?ACT=25&lang=en";
            break;
        case 'DE':
            location.href = location.href + "index.php?ACT=25&lang=de";
            break;
        }

    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.