new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Cloudflare Workers · all subjects

static assets

15 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Browser fragments not evaluated in redirects

Because fragments are evaluated by the browser and not Cloudflare's network, any fragments in the source are not evaluated.

_redirects file location and placement

To apply custom redirects on a Worker with static assets, declare redirects in a plain text file called `_redirects` without a file extension, in the static asset directory of the project. This file will not itself be served as a static asset, but will instead be parsed by Workers and its rules applied to static asset responses.

_redirects file format per line

Each redirect must follow the format: [source] [destination] [code?]. The source is a required string that can be a file path and include wildcards (*) and placeholders. The destination is a required string that can be a file path or external link and include fragments, query strings, splats, and placeholders. The code is an optional number parameter with a default value of 302. Lines starting with # are treated as comments.

_redirects file limits per file

A _redirects file is limited to a combined total of 2,100 redirects: 2,000 static redirects and 100 dynamic redirects. Each redirect declaration has a 1,000-character limit.

_redirects order of application

The order of redirects in a _redirects file matters. If there are multiple redirects for the same source path, the top-most redirect is applied. Static redirects should appear before dynamic redirects. Redirects are always followed, regardless of whether or not an asset matches the incoming request.

_redirects applies only to static assets

Redirects defined in the _redirects file are not applied to requests served by Worker code, even if the request URL matches a rule defined in _redirects. Redirects may need to be applied manually in Worker code, or alternative options such as Bulk Redirects should be explored.

Supported redirect status codes

Supported redirect status codes are 301, 302, 303, 307, and 308. The default status code is 302.

Splat matching in redirects

A splat (asterisk, *) will greedily match all characters on matching. You may only include a single splat in the URL. The matched value can be used in the redirect location with :splat.

Unsupported redirect features

The following redirect features are not supported: rewrites (other status codes), query parameters in source matching, domain-level redirects, redirect by country or language, and redirect by cookie.

Redirects execute before headers

Redirects execute before headers, so in the case of a request matching rules in both files, the redirect will win out.

Proxying limitations

Proxying will only support relative URLs on your site and you cannot proxy external domains. Only the first redirect in the file will apply when using proxying. For example, a request to /a will render /b, and a request to /b will render /c, but /a will not render /c.

Proxying and SEO considerations

Proxying pages can have an adverse effect on search engine optimization (SEO). Search engines often penalize websites that serve duplicate content. Consider adding a Link HTTP header which informs search engines of the canonical source of content, such as Link: </about/faqs>; rel="canonical".

Bulk Redirects for exceeding _redirects limits

Use Bulk Redirects to handle redirects that surpass the 2,100 redirect rules limit of _redirects. Bulk Redirects can work together with _redirects rules. In case of duplicates, Bulk Redirects will run in front of the Worker, where other redirects live.

_redirects example with multiple rules

A complete _redirects file example: /home301 / 301, /home302 / 302, /querystrings /?query=string 301, /twitch https://twitch.tv, /trailing /trailing/ 301, /notrailing/ /nottrailing 301, /page/ /page2/#fragment 301, /blog/* https://blog.my.domain/:splat, /products/:code/:name /products?code=:code&name=:name

Custom 404 pages configuration with assets.not_found_handling

Configuring assets.not_found_handling to 404-page overrides the default serving behavior of Workers for static assets. When an incoming request does not match a file in the assets.directory, Workers will serve the contents of the nearest 404.html file with a 404 Not Found status.

Give your agent this brain