Redirect based on country - Cloudflare Workers

Hello,

I am trying to find a tutorial or instructions on how to set up redirects on Cloudflare Workers based on the country of the visitor. It would be great if there are any step-by-step instructions on how to set this up.

I would appreciate your guidance.
Many Thanks
Konstantinos

Hi @km11 ,

Hope you are doing well, I’m glad to see you here !

Maybe this JavaScript will match your requirement

$.getJSON('https://ipinfo.io', function(data) {
  if(data.country == 'America'){
    location = 'your_redirect_url';
    return;
  }else if(data.country == 'Beijing'){
    location = 'your_redirect_url';
    return;
  }
})

Not sure America or Beijing are countries. But using the standard country code identifiers works

https://developers.cloudflare.com/workers/examples/country-code-redirect/

1 Like

Yes it is a country (U.S. or US) or America, is a country primarily located in North America . According to wikipedia

Why do I just think Chileans might disagree :wink:

1 Like

Or more importantly, the ISO 3166/MA.

No love for Chileans? :slightly_frowning_face:

I heard this the first time
Chileans are people identified with the country of Chile, whose connection may be residential, legal, historical, ethnic, or cultural. For most Chileans, several or all of these connections exist and are collectively the source of their Chilean identity and found this on Wikipedia

Chileans still are Americans :wink:

2 Likes

Then what is country of Chile

A country on the American continent, just like the US is :wink:

1 Like

Oh Great ! Thanks for sharing some knowledge to my brain sandro :grinning:

Neither of these are the names of countries as used in any common geo-location database. While the intention is clear, those comparisons will not work in code. Terms like Oz or 'Merica might make sense in conversation, but they will not work if used in a geo-aware application.

2 Likes

Hi,
thanks for your prompt reply. Can you please let me know where to paste this code?
Thanks
Konstantinos

I would appreciate anyone’s step-by-step instruction on how to set this up successfully.

Thank you
Konstantinos