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

137
Views
Wildcard domain and pathname regex for redirection

I'm trying to get batch redirection working in cloudflare workers. Here's an array of objects with to and from urls, some will be cross domain.

const redirects = [
  {
    "from": "https://some.domain.tld/product/*",
    "to": "https://another.domain.tld/product/*"
  },
]

I'd like to respect the wildcards and try to redirect based on the pathname of the url.

here's my attempt, but this is just for fixed pathnames and not cross-domain. It's kind of hard to test for cross-domain in the workers playground editor.

async function handleRequest(request) {
  const  url = new URL(request.url)
  const host = url.hostname;
  const path = url.pathname;
  const queryStrings = url.search;

  const location = redirects.find(r => {
      return r.from.includes(path);
  });
  
  if (location) {
   return Response.redirect('https://' + host + location.to + queryStrings, 301)
  }
  return fetch(request)
}

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

How can I change this code so it respects redirects with wildcards? Wildcards will most likely always be at the end, some pathnames will not have wildcards, and it will not always be /product but any pathname that's in the redirect object.

Updated after answer:

const redirects = [
  {
    "from": "https://red.domain.com/product-1/*",
    "to": "https://blue.domain.com/product-1/*"
  },
]

async function handleRequest(request) {
    const url = 'https://red.domain.com/product-1/fees';
    const location = redirects.reduce((loc, r) => {
      let match = url.split(r.from.replace("*", "")[1]);
      if (match) return r.to.replace("*", "") + match;
  }, "");
  if (location) {
   return Response.redirect(location, 301)
  }
  return fetch(request)
}

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

result:

https://blue.domain.com/product-1/h,,ps://red.domain.com/produc,-1/fees

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can match the whole URL to your redirects using Array#split(), while getting the part "after" the wildcard back:

console.log(
"https://some.domain.tld/product/foo/bar/baz?test=baf".split("https://some.domain.tld/product/")[1]
)

For URLs that don't match this redirect, the result will be undefined, so:

const redirects = [
  {
    "from": "https://some.domain.tld/product/*",
    "to": "https://another.domain.tld/product/*"
  },
]


async function handleRequest(request) {
  const {url} = request;

  const location = redirects.reduce((loc, r) => {
    let match = url.split(r.from.replace("*","")[1]; // remove wildcard * for matching
    if(match) return r.to.replace("*","") + match;
  },"")
  
  if (location) {
   return Response.redirect(location, 301)
  }
  return fetch(request)
}

addEventListener("fetch", async event => {
  event.respondWith(handleRequest(event.request))
})
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!