带有子域的批量通配符重定向

Bulk Wildcard redirections with subdomains

我正在尝试创建一个 Cloudflare worker,它根据一些输入通配符方向重定向域。

例如,假设输入是这样的:

const inputs = [
  {
    "from": "*.example.com/products/3268-shirts*",
    "to": "https://store.primarydomain.example.com/collections/3268-shirts*"
  }
]

基于此,以下应该可行:

   [
      {
        "input": "https://store.domain1.example.com/collections/3222-shirts",
        "output": "https://store.primarydomain.example.com/collections/3222-shirts"
      },
      {
        "input": "https://store.domain2.example.com/collections/3222-shirt",
        "output": "https://store.primarydomain.example.com/collections/3222-shirts"
      },
      {
        "input": "https://store.domain2.example.com/collections/3222-shirts/site4",
        "output": "https://store.primarydomain.example.com/collections/3222-shirts/site4"
      },
      {
        "input": "https://store.domain2.example.com/collections/3222-shirts/?pf_t_site=the_site",
        "output": "https://store.primarydomain.example.com/collections/3222-shirts/?pf_t_site=site_site"
      },
      {
        "input": "https://store.domain2.example.com/collections/3222-shirts/products/9000-pants?variant=2995454033242234&utm_content=171134",
        "output": "https://store.primarydomain.example.com/collections/3222-shirts/products/9000-pants?variant=2995454033242234&utm_content=171134"
      }
    ]

输入是请求的域,输出是它将重定向到的域。

这是我目前所掌握的,已经很接近了。它重定向,但不是针对这种特定情况,也不是正确的域。

const inputs = [
  {
    "from": "https://store.domain2.example.com/collections/*",
    "to": "https://store.example.com/collections/*"
  }
]

async function handleRequest(request) {
    const {url} = request;
    const location = redirects.reduce((loc, r) => {
      let match = url.split(r.from.replace("*", ""))[1];
      let final = r.to.replace("*", "") + match;
      if (match) return final;
    }, "");
  if (location) {
      console.log(`${url} redirects to ${location}`)
   return Response.redirect(location, 301)
  }
  return fetch(request)
}


addEventListener("fetch", async event => {
  event.respondWith(handleRequest(event.request))
})

我怎样才能更改上面的代码,以便测试 url 可以工作?

以下内容应该可以帮助您前进。请注意 handleRequest() 将匹配第一个 redirect.from。因此,如果目的地应该不同,请将更具体的放在顶部。

var redirects = [
  {
    "from": "oldstore1.example.com", // target a specific sub-domain. Notice, this is before the match all sub-domains of example.com
    "to": "oldstore1.primarydomain.example.com"
  },
  {
    "from": ".example.com", // will match any/all sub-domains
    "to": "primarydomain.example.com"
  }
]

// This will match on the first matching redirects.from.
function handleRequest(request) {
    let visitorDomain = new URL(request.url);
    const location = redirects.find((loc, r) => visitorDomain.hostname.endsWith(loc.from)); // matches on the ending of URL.hostname.

    // check if either no suitable location was matched 
    // OR if the destination matches the current URL, if so, do not perform 301
    if(!location || visitorDomain.hostname === location.to) { 
        console.log('Not going to perform 301'); 
        return //fetch(request); 
    } 

    // A redirect will happen
    const redirectDest = visitorDomain.protocol + '//' + location.to + visitorDomain.pathname + visitorDomain.search;
    console.log('redirecting to', redirectDest);  
    //return Response.redirect(location, 301)
  
}

一些例子

handleRequest({url: 'https://store.domain2.example.com/collections/3222-shirts'});  
  // redirects to: https://primarydomain.example.com/collections/3222-shirts


handleRequest({url: 'https://www.somewhere.oldstore1.example.com/collections/3222-shirts'});  
  // redirects to: https://oldstore1.primarydomain.example.com/collections/3222-shirts


handleRequest({url: 'https://store.domain2.example.com/collections/3222-shirts/products/9000-pants?variant=2995454033242234&utm_content=171134'}); 
  // redirects to: https://primarydomain.example.com/collections/3222-shirts/products/9000-pants?variant=2995454033242234&utm_content=171134


handleRequest({url: 'https://store.domain2.example.com/collections/3222-shirts/products/9000-pants'}); 
  // redirects to: https://primarydomain.example.com/collections/3222-shirts/products/9000-pants?variant=2995454033242234&utm_content=171134

handleRequest({url: 'https://primarydomain.example.com/collections/3222-shirts/products/9000-pants'}); 
  // NOT going to Redirect.  Already at a suitable matching subdomain.domain.