Is there any way to strip route path prefix from workers?


I set up 3 routes to a Cloudflare worker, just like this:***

Among different routes, there are different path layers. But they do the same things in the asterisk part. Like calling is same as

But in the Cloudflare worker API, I can only get the full URL but can’t get what is the route parts of the URL. I want to get the route part of the request URL, as the logic of the left part of the URL is the same among all these routes.

At this time, I can only create an additional environment variable to store all routes and add a check code to match the environment variable. When I update the routes, I also need to update the environment variables.

Is there any way to let the worker program get the asterisk part whatever the route is, except hardcoding routes in the program or env?

No, it’s not exposed or anything. You’d have to have that sort of logic in your worker, perhaps checking for specific known paths and stripping (ex: support two setups, either on base or on /customname and check if the path starts with /customname and stripping if needed