The mechanics for if and what script should run on a request based on the URL.
For workers.dev zones, scripts are managed by the name of the script. A request to your
script-name.subdomain.workers.dev subdomain will run through the active script with corresponding name,
All the logic for routing must be done in the script itself. For example, you can use the router template.
Each script that runs must have a script name, so there is currently no way to run a script on
For zones proxied on Cloudflare*, route patterns decide what (if any) script is matched based on the URL of that request. Requests are routed through a Workers script when the URL matches a route pattern assigned to that script.
Route patterns can be added using the Cloudflare API or by going to the Workers tab after selecting a zone.
Cloudflare Site routes are comprised of:
* A zone that you have registered with some registrar (not workers.dev) and setup Cloudflare to serve as a reverse proxy.
Route patterns look like this:
This pattern would match all HTTPS requests destined for a subhost of
example.com and whose paths are prefixed by
While they look similar to a regex pattern, route patterns follow specific rules:
*which matches zero or more of any character.
example.com/?foo=*are valid route patterns.
www.example.com/*would take precedence over
*.example.com/*when matching a request for
A route can be specified without being associated with a worker; this will act to negate any less specific patterns. For example, consider this pair of route patterns, one with a Workers script and one without:
*example.com/images/cat.png -> <no script> *example.com/images/* -> worker-script
In this example, all requests destined for example.com and whose paths are prefixed by
/images/ would be routed to
worker-script, except for
/images/cat.png, which would bypass Workers completely. Requests with a path of
/images/cat.png?foo=bar would be routed to
worker-script, due to the presence of the query string.
Here is the full set of rules governing route pattern validity:
If your zone is
example.com, then the simplest possible route pattern you
can have is
example.com, which would match
https://example.com/, and nothing else.
As with a URL, there is an implied path of
/ if you do not specify one.
https://example.com/?anything is not a valid route pattern.
If you omit a scheme in your route pattern, it will match both http:// and https:// URLs. If you include http:// or https://, it will only match HTTP or HTTPS requests, respectively.
https://www.example.com/ but not
*.example.com/ matches both
Hostnames may optionally begin with
If a route pattern hostname begins with
*, then it matches the host and all
If a route pattern hostname begins with `*.`, then it matches *only* all
- `*example.com/` matches `https://example.com/` *and* `https://www.example.com/` - `*.example.com/` matches `https://www.example.com/` but *not* `https://example.com/`
If a route pattern path ends with
*, then it matches all suffixes of that