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.
Well, thank you. If that approach will work it will be a breeze (apart from the ■■■ 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.
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”
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.
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.
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”.
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
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.