I use a Cloudflare worker to redirect visitors to the correct website version like this:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
country_code = request.headers.get('CF-IPCountry');
var link;
switch(request.headers.get('CF-IPCountry')) {
case 'TW': // Taiwan
link = "https://www.website.com/twn";
break;
case 'TH': // Thailand
link = "https://www.website.com/tha";
break;
case 'GB': // United Kingdom
link = "https://www.website.com/gbr";
break;
case 'US': // United States
link = "https://www.website.com/us";
break;
default:
link = "https://www.website.com/rotw" // Rest of the world
}
return new Response('', {
status: 301,
headers: {
'Location': link
}
})
}
The problem is that the Google bot gets redirected to website.com/us and thus my Google entry points incoming visitors straight to the /us website. Is there a way to exclude search bots from the country redirect script and route them straight to website.com instead of website.com/countrycode?
Could you do something like this. I haven't tested this.
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
country_code = request.headers.get('CF-IPCountry');
var link;
let userAgent = request.headers.get('User-Agent') || ''
if (userAgent.includes('Googlebot')) {
return new Response('', {
status: 301,
headers: {
'Location': "https://www.website.com/"
}
})
}
switch(request.headers.get('CF-IPCountry')) {
case 'TW': // Taiwan
link = "https://www.website.com/twn";
break;
case 'TH': // Thailand
link = "https://www.website.com/tha";
break;
case 'GB': // United Kingdom
link = "https://www.website.com/gbr";
break;
case 'US': // United States
link = "https://www.website.com/us";
break;
default:
link = "https://www.website.com/rotw" // Rest of the world
}
return new Response('', {
status: 301,
headers: {
'Location': link
}
})
}