Country-Based Redirects

We are wanting to create a country based redirect. I read an article on another website talking about using Workers to create a Java Script redirect and some code was provided. But this was aimed at 403 redirects. We just want to create a general redirect by country. Is there some instructions and/or code to do that posted somewhere?

Here is also the link to the article I read in case it is f any use:

https://403page.com/how-to-use-cloudflare-to-handle-country-based-redirects

Hey! If I read the page you linked correctly it should also just do a regular redirect, but we’ve got an example page as well on the Workers Developers site: Country Based Redirect

You can just update/edit the countryMap object in there to change redirects to other pages and/or add more countries.

1 Like

Thank you and sorry, but I missed that.

I created a Worker, then copied and pasted in the script from the link your provided, changed the country to the one country I want, followed by the link I want to redirect the traffic to, and then I deployed it.

After that I tested it with the country used in the script for the redirection, but it didn’t work. As I am new to the whole Worker thing, did I perhaps leave out a necessary part of the loop to get it working? Do I also need to create a Route in workers first?

Thanks again.

1 Like

Would you be able to post your code changes? The country redirect works for me, I just tested it with this countryMap:

const countryMap = {
  US: "https://cloudflare.com/",
  DE: "https://www.cloudflare.com/de-de/",
}

Maybe the country doesn’t match for you? Here’s a sample page where you can look up the Country key

1 Like

Thank you and very sorry for the late reply. I posted the code I used on the link below for you to see:

https://privatebin.support-tools.com/?ee0206f29bdb29dc#DimQjmCMSHkmtHYvbVHuLbRV1qU/Dh2SiYLFBANfwFI=

I tested it with the country of Moldova (so MD for the country code), then set a VPN to that country and tested it and the redirect didn’t work.

Again, not sure I had setup the Worker correctly or not though. All I did was got to the Workers button on the Cloudflare navigation bar, then Manage Workers > Workers > Create A Worker and then I dropped in that code. Are there any other steps I need to do to deploy the Worker and make the script active?

By the way, at the moment we use .htaccess to redirect certain countries to certain links, but this requires us updating all the country IP addresses every month, which isn’t ideal. So I assume if we had a Worker doing this for us then we wouldn’t need to do anything to maintain the redirects and it would be a time saver for us?

Thanks again.

1 Like

When you open up this page with your client, do you see MD as the value for Country?

When you open the Workers page in your Dashboard and select your Worker, you should also see a route as well as on/off toggle for the Worker to make sure it’s enabled. Also make sure that the contents in the script editor were saved.

If you click this link, are you being redirected to cloudflare.com?

2 Likes

Thank you very much your reply on this again. Please give me just a bit of time to confirm everything you asked about and I will come back again and provide more answers. Many thanks.

1 Like

OK, I have now checked everything you asked about. Yes, the link you provided does show MD as my country. I also double checked my country to confirm using https://whatismyipaddress.com/ and so all is good there.

Yes, I also see a route as you mentioned when I open my Workers page in the Dashboard and the toggle is turned on. If I click the route link itself I get rerouted to the redirect URL created in the link so I think this also confirms that the contents of the script editor were saved and are working correctly.

Also, if I click your link I do get redirected to cloudflare.com

One thing though which I am not sure if it is a problem or not. If I reopen the Worker script code I created there is a button on the right of the script code which says Send. If I click Send I get the following error below it inside the tab area called Console:

worker.js:8 Uncaught (in promise) TypeError: Cannot read property ‘country’ of undefined
at redirect (worker.js:8)
at handleRequest (worker.js:28)
at worker.js:32
redirect @ worker.js:8
handleRequest @ worker.js:28
(anonymous) @ worker.js:32

One thing I also didn’t ask you, which I am assuming is correct though, in the script where I have the two letter country listed, I assume this is where I put the URL that I want all visitors from that country redirected to, correct?

I also setup a route to the script I created and for the route I used .mydomain.com/, is this correct?

Anyway, all seems set up correctly as far as I can see. Just not redirecting as it should be it seems.

Thanks again.

1 Like

I have now tested on another device and can confirm everything is working correctly as intended. Thank you. Seems it is caching issue on the device I was running the first tests on even though I had cleared the cache.

By the way, do you happen to know how often the IP databases are updated which Cloudflare uses to correspond with the country codes being used in these types of scripts?

Than you again.

1 Like

We setup redirects for a number of countries and have run tests for 24 hours utilizing tracking links to see if the country redirects are working correctly. Unfortunately it seems that the redirects only work for some of the countries we have selected and not the others, but we don’t know why. So for the time being we have gone back to the method we were using before of utilizing the .htacess file, and combined with IP addresses for each country that we want to redirect which we obtain from ip2location.com, and then we update the list once per month. This setup isn’t ideal for us, but until we can be sure that the country redirects from the Workers are operating correctly, then we can’t afford to rely upon them. Thank you.

1 Like

Can you elaborate on

it seems that the redirects only work for some of the countries we have selected and not the others

You mean that the Country is in the list but the redirect doesn’t work or that it’s just a white page? The example worker doesn’t return anything if the country doesn’t match to anything in the list, you’d need to add some code to do a redirect to a generic page if there’s no match.