Redirect based on country - Cloudflare Workers


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

Hi @km11 ,

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

Maybe this JavaScript will match your requirement

$.getJSON('', function(data) {
  if( == 'America'){
    location = 'your_redirect_url';
  }else if( == 'Beijing'){
    location = 'your_redirect_url';

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

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:


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.


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

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

Thank you