Redirect a visitor when gets blocked by GEOIP

Hello everyone, i’m Juan and i’ve been looking for this with Google but with no luck. I was wondering how to make my users use a specific domain for each country by just using cloudflare. More clearly, I have two domains - one with .eu TLD and the other with .la . What I want to achieve is redirect a european user coming to .la domain to the respective .eu domain, just using cloudflare. That is because i’m using firewall to block .la access to users outside latin america.

Does anyone have ever did that? if so, I will appreciate any help.


The Cloudflare firewall?

Not sure if there is a better way and I would certainly be interested to know if there is one –

But one solution is CF workers. All workers get a cf-ipcountry header and they can redirect accordingly.

async function handleRequest(request) {
  let host = new URL(request.url).hostname;
  const cc = request.headers.get('cf-ipcountry').toLowerCase();  
  const  ret = `Redirect from ${host} to ${host}.${cc}`;

//return a 302 instead of course; this is just to demo
  return new Response(ret, {status: 200});   
1 Like

Yes, the same. Thanks for your help.

That could be a candidate solution. If I use your proposal, maybe in order to make a change I will need to burn the countries on the code. That’s because I’m using .la for every country in latam and .eu for europe’s countries. Could be. Thank you!

I was looking for a more integrated solution.

That would be a bit of an issue then, as the firewall will always block the requests and they will never reach any possible redirect.

For starters, you’d have to remove that block. Then, you need to decide where you’d like to implement the redirect. Personally, I’d probably opt for server-side, however a worker would be a possibility as well, in which case the following might just do the trick

const EUROPE = new Set(['AD', 'AL', 'AT', 'BE', 'BG', 'BY', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FO', 'FR', 'GA', 'GB', 'GG', 'GH', 'GI', 'GL', 'GR', 'HR', 'HU', 'IE', 'IM', 'IS', 'IT', 'JE', 'LI', 'LU', 'LV', 'MC', 'MD', 'ME', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO', 'RS', 'RU', 'SE', 'SI', 'SK', 'SM', 'UA', 'VA']);

return EUROPE.has( ? Response.redirect('') : fetch(request);

I will try this and be back with the results. Thank you!

Even though it seems to work, there is probably an await missing before the fetch. But overall the solution should work.

This topic was automatically closed after 14 days. New replies are no longer allowed.