Country Redirection

I would appreciate a little help.

I have tried to implement country redirection with the url followed by country code, based on the following support articles, but nothing works


and various other articles (limited to 2 links only for the moment).
Always ending with too many redirects errors.

Basically I am trying par example to redirect from italy to mysite.com/it/
while leaving from any other country the default mysite.com/.

Any help would be greatly appreciated.
Thanks in advance.

As mentioned in that thread there is a worker template at GeoIP redirection worker. What is that doesnt work?

As mentioned in that thread in June '19 by it1109

If I change this URL to www.website.com/eu I get

Uncaught (in promise) Error: internal error
at Redirect (VM8 worker.js:65)
at VM5 worker.js:13

Looks like it’s because of the “/”. Any idea how to fix this?

Untested (and a bit rough), but have you tried this?

Also, make sure it is mapped only to / without any wildcards, otherwise you enter a loop.

async function handleRequest(request)
{
	return request.cf.country == 'IT' ? Response.redirect(request.url + '/it') : fetch(request);
}

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

Well, that most probably was just the solution I’ve been looking for.
I will have to check for a few edge cases, but seems to be working just fine, so not rough at all, on the contrary it works wonders.
You, sir, are a life saver.

Just to clarify, this seems to be working when cloudflare is set in development mode, or when I clear all caches.
Is that supposed to be the case?
What if I pause cloudflare altogether?

I really appreciate all your help, it was invaluable.
Thank you again.

Absolutely, caching shouldnt influence the invocation of a worker.

In that case no, as Cloudflare will return the origin IP to DNS requests in that case and hence requests will not go via the proxy and so the worker will not be invoked either.

My pleasure :slight_smile:

1 Like

Well, after some testing, I have encountered a few setbacks.

  1. The redirection works 100% and all the time, not just on first visit.
    Even after changing languages via the menu, or hitting the address directly,
    the redirection always happens, so there is no way to visit the mysite.com/ if coming from the country in the redirection (italy in my case), it always redirects to mysite.com/it.
    Is there a way to respect session cookie, or just redirect only on the first visit?

  2. And is there a way, if coming from external source and hitting a subpage directly to redirect to that subpage too in the country language?
    So if from italy and hitting directly mysite.com/company/services to redirect to mysite.com/it/company/services ?
    But only on first visit, as well.

Just a few minor annoyances, but it would be great if they could be addressed as well.
In any case, thank you very much.

Sure, that is what it is supposed to do.

If you want the redirect dependent on something you’d need to extend the current code to take that into account as well.

In that case you’d need to map the worker using a wildcard, but you’d still need to extend the code to avoid an infinite loop and not redirect when you are already on “/it”.

If it’s not too much to ask, any helpful hints maybe?
Mainly for the first part, the ability to redirect just on first visit, and then respect the session language.

Thanks again.

You could use cookies, which you set in the response object and then check for in the request object.

I can’t seem to find any useful info.
It seems that the /countrycode is tricky and not as widespread as I thought.
Would you be able to provide any helpful documentation/code snippet I can look into?

Thanks in advance.

The Worker API definitely is a good start

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