Redirecting specific country to sub domain


#1

I am considering creating a sub domain to be served in place of my actual domain to traffic from a specific country only. So when visitors from that country attempt to visit ‘mydomain.com’ they are instead redirected to ‘country.mydomain.com’, whilst the rest of the world is not redirected. Among the matters for consideration is of course the small question of actually how to do this.

Any rough pointers as to method would be helpful and appreciated. I am on the Cloudflare free subscription at the moment. My domain is hosted on a VPS to which I have root access.


Per country page rules
#2

There’s nothing in the CF control panel that’ll allow you to do this. At least in the free plan that I know of (eg:page rules).

Assume PHP (but any language would do) you’d probably want to get the country code header from CF and redirect based on that.

example

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

if($country_code == "US"){
// American
header("Location: https://americans.example.com");
die();
}

#3

Well, thank you. If that approach will work it will be a breeze (apart from the fag of making the sub domain and all the page variations). I was imagining something far more complicated with tables of a whole countries’ ip addresses.

I’ve got a couple of months before I need to decide whether to go ahead, so that’ll give me some time to set up a sub domain and see if I can get it to work.

After a little search based on your code example I came across this variation on Github https://gist.github.com/bwhli/5d744eb0400edc333c0f/1d818bdb86813c16b55ad3468a19facca13edb6f


#4

Well, if you want multiples and are happy with those subdomains then I would probably only change the code you posted slightly with…

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
$link = "";

switch ($country_code){
    case "US":
    case "GB":
    case "HK":
        $link = "http://" . strtolower($country_code) . ".example.com";
    break;
}

if($link!=""){
    header("location:$link");
    exit;
}

#5

Thank you again for your replies. I posted the multiple variation really only because I suddenly realised that other people had wanted to do something similar before me using Cloudflare. I thank you for your code.

I should preface what I am about to say by admitting that this is not my day job, and I know far less about coding than most people who will read this. Anyway, this is what I learned in testing this out. I haven’t yet implemented it for other reasons like actually constructing the country specific pages.

First my website is constructed in html for reasons of security. There is no database or backend, so nothing to hack. However I could insert the redirect by renaming index.html to something else and inserting the geolocation redirect as index.php. That worked, but obviously a visitor from the country I wanted to redirect could go directly to any other page in the website and see the International version unless I also manually created a php redirect file for that page too. And there’s more than a 100 pages so it would be tedious to have to do every page. So after a little more research I hit on using the htaccess file to effect the redirect.
I used this code after “RewriteEngine on”

SetEnvIf CF-IPCountry “(.*)$” Country=$1

RewriteCond %{ENV:Country} BE

RewriteRule ^(.*)$ https://be.example.com/$1 [R,L]

In this example I am redirecting Belgium (CF code is ‘BE’) to subdomain be.example.com

This worked for me in my test. Although I had to first turn off a “cache everything” page rule I was using. And of course it is necessary to make sure that in Cloudflare under the ‘Network’ tab you have ‘IP Geolocation’ set to on.

The real hard work that makes this possible has been done by Cloudflare who have added the Geolocation header.

Thank you user 7271. I’m so glad I asked on this forum rather than elsewhere since because I am already using Cloudflare there was such a straightforward solution readily to hand.


#6

Hello Bill, was going through your post here as i was searching for the same solution and decided to use Citrix GSLB feature but for that i need my subdomain delegation from my actual domain parent zone hosting provider but they refused to do that which made me to look out for other option or service providers.
Could you please confirm if you succeed in achieving this using CF, If yes please share the steps so that will do the same for my domain.


#7

So, in your website “public_html” folder open the .htaccess file and add this code at the top immediately after “RewriteEngine on”.

  1. SetEnvIf CF-IPCountry “(.*)$” Country=$1
  2. RewriteCond %{ENV:Country} BE
  3. RewriteRule ^(.*)$ https://be.example.com/$1 [R,L]

On line 2 change “BE” to whichever country code you want to block. “BE” is Belgium, but probably you don’t want to block them. The CF country codes can be found here

On line 3 change the url from “https://be.example.com” to whatever sub domain you want to redirect to.

That’s it.
Except I had to first turn off a “cache everything” page rule I was using. And of course it is necessary to make sure that in Cloudflare under the ‘Network’ tab you have ‘IP Geolocation’ set to “on”.


#8

Hello Bill
thanks for your prompt reply. Could you please share the steps to follow if Webserver is IIS instead of Apache? as i guess htaccess will not work on IIS
My website is hosted on on prim windows server


#9

Well Rakesh, I’m afraid I cannot help you with that. As I remarked earlier, this is not my day job. I’m just thrashing around like a lot of others. And I’ve never used anything but Apache.
However I’m sure someone else will know either on here or on another forum specifically dealing with IIS. Since CF is already adding the geo-location header it’s just a matter of reading it and redirecting the traffic.


#10

Looks like there are some examples here and a plugin which mimics the capability


#11

This topic was automatically closed after 14 days. New replies are no longer allowed.