Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
Redirecciones masivas de comodines con subdominios

Estoy tratando de crear un trabajador de Cloudflare que redirige los dominios en función de algunas direcciones de comodín de entrada.

Por ejemplo, digamos que la entrada es esta:

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

Basado en esto, lo siguiente debería funcionar:

 [ { "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" } ]

La entrada sería el dominio solicitado y la salida es el dominio al que se redirigirá.

Esto es lo que tengo hasta ahora para esto, que está cerca. Redirecciona, pero no para este caso específico, y no con los dominios correctos.

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

¿Cómo puedo cambiar el código anterior para que funcionen las URL de prueba?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Lo siguiente debería ser capaz de ponerte en marcha. Tenga en cuenta que handleRequest() coincidirá con el PRIMER redirect.from. Por lo tanto, coloque los más específicos en la parte superior si el destino debe ser diferente.

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

Algunos ejemplos

 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.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda