How can i redirect users (geolocation IP) from Italy to another site?

Hi everyone.

I have a site that i want to create a rule.

Rule:
When someone with an IP originating from Italy visits my site (mySiteExample.com) to be redirected to (mySiteExample.net).

Can someone provide me the steps?

Much appreciated!!!

Hi @apitziolis,

If you want to use Cloudflare for this, you would most likely need Workers. Something like this:

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

1 Like

Thank you @domjh for your swift response.

Now, lets say that i want to redirect users from Italy, to visit: https://websiteONE/

Is this code OK? Or is it missing something?

C O D E:

/**

if (country != null && country in countryMap) {
const url = countryMap[country]
return Response.redirect(url)
}
else {
return await fetch(request)
}
}
/**

async function handleRequest(request) {
return redirect(request)
}

addEventListener(β€œfetch”, event => {
event.respondWith(handleRequest(event.request))
})

The forum does not format that code nicely :grinning_face_with_smiling_eyes: Next time, try using ``` above and below your code to format it as code.

That all looks OK. I have not tried this Worker myself, but it looks like it matches the example which should work. You then just need to define the route for it as the site you want to redirect from.

1 Like

Like this, @apitziolis.

/**
  Returns a redirect determined by the country code
  @param {Request} request
*/
async function redirect(request) {
  // Use the cf object to obtain the country of the request
  // more on the cf object: https://developers.cloudflare.com/workers/runtime-apis/request#incomingrequestcfproperties
  const country = request.cf.country
  if (country != null && country in countryMap) {
    const url = countryMap[country]
    return Response.redirect(url)
  } else {
    return await fetch(request)
  }
}

/**
  A map of the URLs to redirect to
  @param {Object} countryMap
*/
const countryMap = {
  IT: "https://websiteONE/",
}
async function handleRequest(request) {
  return redirect(request)
}

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})
1 Like

thank you everyone for your really swift responses! MUCH appreciated!

I get an error (Code: 10021) @ line 5. Any ideas on this?

I just copied the exact code from @matteo’s post above and can confirm it works for me and the redirect works as expected.

Can you copy that or check that yours exactly matches it?

1 Like

It does match exactly, but still there is an error.

I would highly appreciate it if someone lets me know why i get these errors when i used the same exact code.

I would suggest you go back and copy the original code sample I linked to again or copy the code @matteo pasted directly, as I have checked that and it works.

Otherwise, copy and paste your entire code from the Worker in code formatting here so we can check it properly.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.