Execute specific worker based on url path

Hi all, simple request that, for some reason, I’m having a hard time finding a solution to:

If I have a custom domain purchased through CF and, when somebody types in customdomain.com/path1 I want it to trigger Worker1, how would I do this?

I can add the custom domain to the worker overall but, this appears to be a catch all rather than a specific route.

Similarly customdomain.com/path2 should route to Worker2… and customdomain.com without a path should 404.

Please let me know!

You add a specific route to the worker e.g.

1 Like

Alright, so I removed the custom domain from under the trigger section, and then added the route and zone to the routes section just below it like this:
domain.com/folder/path* with an asterisk at the end to deal with any parameters passed. Now when I attempt to hit that endpoint with or without params it says site can’t be reached.

Can you provide an example of what you are trying to do?

I’m just trying to make it so that when somebody visits mydomain.com/matchedpath?param1=value1 it triggers a specific worker and when somebody visits mydomain.com/matchedpath2?param1=value1 it triggers another, and when somebody visits just mydomain.com they are returned a 404 or served a not found, that’s all.

With route example.com/test* the addition of parameters makes no difference, the route it still valid and available. Same for route example.com/a/b*.

Route example.com/test* the following work


Route example.com/a/b* the following work


Right, I figured this was the case, however when I removed custom domains from this section here ALL routes broke and, when i add it back in, visiting the root domain will trigger a worker when it should do nothing. Shown here because i can’t add images for some reason https://ibb.co/xzB38qD

Can you share the domain and/or route?

krakentrk.com/tp/ma* should run a specific service worker for that path.

I just added the custom domain back, so if you visit that path above (or any other path on the domain, because a custom domain seems to make paths useless) it will run the worker and redirect you to an end site.

If you add a custom domain it will capture all paths, parameters, etc. If you only want it to run on a specific route or specific routes, remove the custom domain and only add the routes.

I know it works. I’ve done it. I cannot guess why it doesn’t work for you because I cannot see the complete setup of your account and worker.

That’s what I thought, but when I remove the custom domain everything breaks.

Worker for reference

export default {
  async fetch(request) {
    const base = "https://redirectedsite.com/redirect-path";
    const statusCode = 301;
    const { searchParams } = new URL(request.url)
      const url = new URL(request.url);
      const { pathname, search } = url;
      const destinationURL = `${base}${search}`;
      return Response.redirect(destinationURL, statusCode)  

Pretty stumped, maybe a propagation thing?


You’ll likely run into caching issues because you are using 301 Moved Permenantly rather than a 302 Found or 307 Temporary Redirect. This could explain why the root domain continues to redirect after you removed it.

The code, while working, isn’t efficient as there are duplication of new URL(request.url) and unused variables.