GeoIP redirection worker

That would the Cloudflare Worker.

create a new coludflare workers and past in!

Hello guys,

I need your help urgently. looking at the path, or the full URL instead of hostname
I saw your comments and solution for the path and I tried to apply as Sandro example above but is not worked for my case. Below my code.

//Africa, these countries go to .fr
var af_countries = [‘MA’,‘GN’,‘TG’,‘MG’,‘CM’,‘CI’,‘BJ’,‘BI’,‘TD’,‘CD’,‘CG’,‘ML’,‘SC’,‘DJ’,‘SN’,‘CY’];
//UK, these countries go to .eu
var uk_countries = [‘GB’,‘JE’,‘IM’,‘GG’];
//IE, these go to .eu
var ie_countries = [‘IE’];
//Europe, these go to .eu. Everyone else goes to .com
var eu_countries = [‘AT’,‘BE’,‘BG’,‘CZ’,‘DE’,‘DK’,‘EE’,‘ES’,‘FI’,‘FR’,‘GB’,‘GR’,‘HR’,‘HU’,‘IE’,‘IT’,‘LT’,‘LU’,‘LV’,‘MT’,‘NL’,‘PL’,‘PT’,‘RO’,‘SE’,‘SI’,‘SK’,‘GI’,‘IS’,‘LI’,‘MC’,‘NO’];
//these IP addresses don’t get redirected
var bypass_ip = [‘xxx’, ‘xxx’,‘xxx’,‘xxx’];
//user agents containing any of these strings don’t get redirected
var bypass_ua = [‘googlebot’,‘bing’, ‘Bingbot’ , ‘Slurp’, ‘DuckDuckBot’ , ‘Baiduspider’, ‘YandexBot’, ‘facebot’, ‘ia_archiver’, ‘MSN Bot/BingBot’, ‘Soso Spider’, ‘ExaBot’, ‘Sogou Spider’, ‘Google Plus Share’ ,‘Google Feedfetcher’, ‘Facebook External Hit’];

addEventListener(‘fetch’, event => {
event.respondWith(Redirect(event.request))
})

function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}

function byPass(request){
if (inArray(request.headers.get(‘CF-Connecting-IP’),bypass_ip)){
return true;
}
var length = bypass_ua.length;
for(var i = 0; i < length; i++) {
if(request.headers.get(‘user-agent’).includes(bypass_ua[i]))
return true;
}
return false;

}

function mapCountry(country_code){
if (inArray(country_code,uk_countries)){
return ‘domain.com’;
}
if (inArray(country_code,af_countries)){
return ‘domain.com/fr’;
}
if (inArray(country_code,ie_countries)){
return ‘domain.com/ie’;
}
if (inArray(country_code,eu_countries)){
return ‘domain.com/eu’;
}
//everyone else gets to .com website
return ‘domain.com’;
}

/**

  • Fetch and log a given request object
  • @param {Request} request
    */

/**

  • Fetch and log a given request object
  • @param {Request} request
    */
    async function Redirect(request) {
    var url = new URL(request.url);

correctHost = mapCountry(request.headers.get(‘CF-IPCountry’));
if (byPass(request)){
correctHost = url.hostname;
}
if (correctHost !== url.hostname){
url.hostname = correctHost;
console.log(‘redirecting to ‘+url.href);
return new Response(’’, {
status: 301,
headers: {
‘Location’: url.href
}
})
}

console.log(‘no redirect’);
const response = await fetch(request)
return response
}

1 Like

Hi, I am using xxx.com/us but it’s not working, getting Worker threw exception error ( Error 1101)

Any clue?

Hi, I am using xxx.com/us but it’s not working, getting Worker threw exception error ( Error 1101)

Any clue?

you can redirect only between domains, with this script… but I’m not sure, I’m not an expert, sorry

Will this script work if I change the redirected domain eg xxx.xxx.com to a specific file url, such as xxx.xxx.com/imagetodisplay.jpg ?

If not, how could I achieve this?

Thanks.

Sorry for the late reply. Anyway this script works only with domains, not pages

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