是否可以从 Cloudflare worker 重定向脚本中排除搜索机器人(如 Google)?
Is it possible to exclude search bots (like Google) from Cloudflare worker redirect script?
我使用 Cloudflare worker 将访问者重定向到正确的网站版本,如下所示:
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
}
})
}
问题是 Google 漫游器被重定向到网站。com/us 因此我的 Google 入口将访问者直接指向 /us 网站。有没有办法从国家/地区重定向脚本中排除搜索机器人并将它们直接路由到 website.com 而不是网站。com/countrycode?
你能做这样的事情吗?我还没有测试过这个。
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
}
})
}
我使用 Cloudflare worker 将访问者重定向到正确的网站版本,如下所示:
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
}
})
}
问题是 Google 漫游器被重定向到网站。com/us 因此我的 Google 入口将访问者直接指向 /us 网站。有没有办法从国家/地区重定向脚本中排除搜索机器人并将它们直接路由到 website.com 而不是网站。com/countrycode?
你能做这样的事情吗?我还没有测试过这个。
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
}
})
}