Javascript 是否可以从Cloudflare worker重定向脚本中排除搜索机器人(如Google)?

Javascript 是否可以从Cloudflare worker重定向脚本中排除搜索机器人(如Google)?,javascript,cloudflare,googlebot,cloudflare-workers,Javascript,Cloudflare,Googlebot,Cloudflare Workers,我使用Cloudflare worker将访问者重定向到正确的网站版本,如下所示: addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) }) /** * Respond to the request * @param {Request} request */ async function handleRequest(request)

我使用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
          }
        })
  }

问题是谷歌机器人会被重定向到website.com/us,因此我的谷歌入口会将访客直接指向website.com/us网站。有没有办法将搜索机器人从国家/地区重定向脚本中排除,并将它们直接路由到website.com而不是website.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
          }
        })
  }

你能这样做吗。我还没有测试过这个

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
          }
        })
  }

看一看,robots.txt是由服务器上运行的php软件动态生成的。更改它目前超出了我的范围(很遗憾)。您不能将worker保存在robots.txt“protects”目录中吗?请查看。robots.txt由服务器上运行的php软件动态生成。更改它目前超出了我的范围(很遗憾)。你不能将worker保存在robots.txt“protects”目录中吗?看起来不错!let userAgent=request.headers.get('User-Agent')之后的| | |是什么意思?很高兴为您提供帮助。|这只是检查null的一种简捷方法。如果request.headers.get('User-Agent')解析为null,则它将用空字符串代替。在第二行停止出错。看起来不错!let userAgent=request.headers.get('User-Agent')之后的| | |是什么意思?很高兴为您提供帮助。|这只是检查null的一种简捷方法。如果request.headers.get('User-Agent')解析为null,则它将用空字符串代替。在第二行停止出错。