skip to content

Why do rules near the end of a Cloudflare Pages _redirects file do nothing when it opens with a /blog/* splat rule followed by 300 exact-path redirects?

level: seniorimportance: should knowfreq 24%

answer

  1. two separate rule budgets
  2. static versus dynamic rules
  3. order changes how lines are counted
  4. skipped lines, not a failed deploy

basics

~20 s

Pages allows 2,000 static and 100 dynamic _redirects rules, and once the first splat or placeholder rule appears, every later line counts as dynamic. The splat plus the first 99 exact paths use the dynamic budget; the remaining lines are skipped.

solid answer

~40 s

A `_redirects` file has two budgets: **2,000 static** rules (exact paths) and **100 dynamic** rules (a `*` splat or a `:placeholder`), 2,100 in total. Rules are counted in file order, and after the first dynamic rule every following rule is counted as dynamic, even an exact path. Here `/blog/*` is dynamic rule 1, the first 99 exact paths fill the dynamic budget, and Pages skips the rest of the file. Nothing fails: the deployment goes out and those paths simply do not redirect, while `wrangler pages dev` prints the skip as a warning locally. The fix is to put every exact-path rule first and the dynamic rules last, and to move to Bulk Redirects if the list ever exceeds what `_redirects` can hold.

code

plaintext · 4 lines
plaintext
✨ Parsed 100 valid redirect rules.
▲ [WARNING] Found 1 invalid redirect rule:

  ▶︎ Maximum number of dynamic rules supported is 100. Skipping remaining 2 lines of file.

go deeper

for a junior

Recall the line format, source then destination then an optional code, and that 302 is the default status.

for a middle

Explain static versus dynamic rules, their 2,000 and 100 limits, and that the top-most of two duplicate sources wins.

for a senior

Diagnose silently skipped rules from file order, verify with the local dev server's redirect summary, and know which redirects belong in Bulk Redirects instead.

for a principal

Set a rule for where redirects live, the site's file for a short list and an account-level product for large migrations, and check it in CI.

## The `_redirects` file **Cloudflare Pages** reads a plain-text file named `_redirects` (no extension) from the root of the **build output directory**. Frameworks usually copy it there from their `public/` or `static/` folder. Pages parses it as configuration rather than serving it, and applies its rules to requests for static assets. Each line is: ```plaintext [source] [destination] [code?] ``` - `source` is a path, optionally with one `*` **splat** or named **placeholders** such as `:slug`; - `destination` is a path or a full URL and may reuse `:splat` and placeholders; - `code` defaults to `302`; `301`, `302`, `303`, `307` and `308` redirect, and `200` serves the destination's content under the original URL (proxying, relative paths only). Lines starting with `#` are comments. A malformed line, or one with another status code such as `404`, is ignored on its own. ## Static and dynamic rules have separate budgets | Kind | What makes a rule this kind | Limit per file | |---|---|---| | **Static** | an exact source path | 2,000 | | **Dynamic** | a splat or placeholder in the source | 100 | Each declaration is also capped in length (the docs give 1,000 characters). The detail that breaks migrations is how lines are counted. Pages walks the file top to bottom, and **once it meets the first dynamic rule, every later rule counts as dynamic**, whether or not it contains a splat. The docs put it as "static redirects should appear before dynamic redirects". ## Walking through this file 1. Line 1, `/blog/* /posts/:splat 301`, is dynamic rule 1. 2. The next 99 exact-path lines are counted as dynamic rules 2 to 100. 3. The next exact-path line would be dynamic rule 101, so Pages stops and skips the rest of the file. About 200 redirects vanish. The deployment still succeeds; the affected paths fall through to normal asset serving, which typically means a 404 page or, without a top-level `404.html`, the single-page-app fallback to `index.html`. Locally, `wrangler pages dev` prints a warning such as `Maximum number of dynamic rules supported is 100. Skipping remaining ... lines of file.` and suggests moving a line "above any lines with splats or placeholders". ## How splats and placeholders match - A **splat** (`*`) matches greedily, and a source may contain only one; its value is reused in the destination as `:splat`. - A **placeholder** is a colon followed by a letter and then letters, digits or underscores, such as `:year` or `:post_id`. It matches everything up to the next `/` in a path, and each named placeholder may be referenced once. - Fragments in a source are never evaluated, because the browser does not send them; fragments and query strings are allowed in a destination. ## Other rules of the file worth knowing - **Duplicates**: if two lines share a source, the top-most applies and the later one is ignored. - **Assets do not win**: a redirect is followed even when a file exists at the source path. - **No chaining of proxies**: with `/a /b 200` and `/b /c 200`, a request to `/a` renders `/b`, not `/c`. - **No `/* /index.html 200`**: Pages rejects it as a loop, because it already strips `/index.html` from URLs. A site without a top-level `404.html` gets single-page-app fallback anyway. - **No conditions**: there is no matching on query strings, hostnames, country or cookies in `_redirects`. - **Functions are not covered**: `_redirects` applies to static asset responses, not to requests that Pages Functions answer (the lesson on Pages Functions covers that layer). ## Fixing it 1. Put **all exact-path rules first** and the splat and placeholder rules **last**. 2. Keep the dynamic rules at or under 100; replace a long tail of similar patterns with one broader splat where you can. 3. Run `wrangler pages dev` on the build output and read its redirect summary before you push. It reports how many rules parsed and lists every ignored line with its reason. 4. If the list outgrows 2,000 static rules, move it to **Bulk Redirects**, an account-level redirect product that runs in front of the Pages project; the lesson on redirect rules covers it. ```plaintext # exact paths first /pricing-2023 /pricing 301 /about-us /about 301 # dynamic rules last /blog/* /posts/:splat 301 /docs/:version/:page /docs/:page 302 ```

  • Why does Pages reject `/* /index.html 200` in `_redirects`?
    Pages serves `/index.html` at `/` and redirects requests for `/index.html` to strip that suffix, so a catch-all to `/index.html` would loop. It is ignored with a loop warning. A single-page app does not need it: without a top-level `404.html`, Pages already answers unknown paths with `index.html`.
  • A site needs 5,000 legacy redirects. What do you use?
    Not `_redirects`, which holds at most 2,000 static and 100 dynamic rules. Use Bulk Redirects, which runs in front of the Pages project and can work alongside a smaller `_redirects` file; the redirect-rules lesson covers it. Keep `_redirects` for the site's own short list.
  • Do `_redirects` rules apply to a route that Pages Functions answers?
    No. `_redirects` is applied to static asset responses only. If a Function matches the URL, the redirect is not applied, so either move that redirect into the Function's code or exclude the path from Functions.

  • The only limit is 2,100 rules, so rule order does not matter.
  • Exceeding the redirect limit makes the Pages deployment fail.
  • A file at the source path takes priority over a redirect rule.
  • A `404` status code in `_redirects` serves a custom not-found page.
  • Adding `/* /index.html 200` is required for a single-page app on Pages.