Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!