Redirect user to subdomain for specific country

I have a website www.xyz.com which has the content for a particular country X. Also I have another site target to different country Y on a subdomain of this very same domain like subdomain.xyz.com

I can use any free geo ip database and some php script to achive the redirection and I did this successfully but now the problem is

Read More

Person from country Y visiting the site www.xyz.com is being redirected to subdomain.xyz.com but he is suppose want to check www.xyz.com it again redirects them to subdoamin.xyz.com.

I want this the google way, so that if suppose a person visiting google.com from UAE is being redirected to google.ae but if they click go to Go to Google.com they are redirected to google.com without any problem.

I need to implement this for my site..

The site is in wordpress. Please suggest me the best possible solution.

Ok Here is a code that I have used..

/**
 * Example 6
 *
 * This is what google probably do with users around the world
 *
 * Multiple declatations
 */

// Start by including CountryDetector class
include("./src/CountryDetector.class.php");

// You dont need to create instance of this class you can access only on static context

// Google.co.uk
CountryDetector::redirect("AE", "http://uae.jobscow.com");

// Google Germania
CountryDetector::redirect(array("DE", "AT"), "http://google.de"); // Ausrian and German users

// Google China
CountryDetector::redirect("CN", "http://google.cn");

// Google sweden
CountryDetector::redirect("SE", "http://google.se");

// Google norwegia
CountryDetector::redirect("NO", "http://google.se");

// Google serbia
CountryDetector::redirect("RS", "http://google.rs");

// AND SO ON...

// Finally: Google.com - international
CountryDetector::redirect("international", "http://google.com");

?>

What changes would you suggest me to make in this

Related posts

Leave a Reply

4 comments

  1. Use referer check: if they arrive on http://www.xyz.com and referer is not the country site, redirect to country site.

    If they click on “go to http://www.xyz.com” on the country site, the referer will be country.xyz.com, and then you don’t redirect.

    You could also use a cookie to store a customer’s preference in this matter.

    RFC2616 section 14.36 explains the Referer header field, and this article shows how to use it in php.

    EDIT: as an alternative – because as lars states the user or something along the path could deactivate it / remove the header – you could use a virtual “landing page” on http://www.xyz that hints the server that the visitor came from the country server:

    <a href="www.xyz.com/fromcountryserver.php">Go to xyz</a>
    

    or why not, add a request variable that indicates that the browser came from the country server. Silly example just to show the intention

    <a href="www.xyz.com/index.php?comingfrom=countryserver">Go to xyz</a>
    

    or a mix of these techniques. A bit more work but unless some maniac rewrites URLs clientside it should always work.

  2. I believe it’s just a cookie that Google is using for this silly trick.

    Let me suggest you to navigate to google while some HTTP sniffer on. That’s extremely educational enterprise.

  3. Use redirections on your webserver (i.e. Apache if you use it) or insert them on the pages via javascript (redirect function).

    If you are using Apache, you can use mod_alias to configure your redirections if you are using MaxMind for getting the country of access (you can use other services, I suppose there is no problem on this), i.e:

    GeoIPEnable On
    GeoIPDBFile /path/to/GeoIP.dat
    
    # Redirect one country
    RewriteEngine on
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
    RewriteRule ^(.*)$ http://www.canada.com$1 [L]
    
    # Redirect multiple countries to a single page
    RewriteEngine on
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|US|MX)$
    RewriteRule ^(.*)$ http://www.northamerica.com$1 [L]
    

    More info: http://httpd.apache.org/docs/current/mod/mod_alias.html

    You can also get the client’s address via PHP and construct a javascript function like this (but the Apache solution is more elegant):

    function redir()
    {
    if(checkCountryViaIp(ip) == countryx)
    {
        window.location = "countryxpage.php"
    }
        //etc...
    }