GeoIP redirection worker

This solution from https://community.cloudflare.com/u/sandro
seems to work perfectly.

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