skip to content

After a Cloudflare URL Rewrite Rule stripped /api from request paths, an Origin Rule matching /api/ stopped firing; why, and what fixes it?

level: seniorimportance: should knowfreq 22%

answer

  1. phases run in a fixed order
  2. rewrite runs before Origin Rules
  3. fields change between phases
  4. raw fields never change
  5. Cloudflare Trace shows each match

basics

~10 s

URL Rewrite Rules run in the http_request_transform phase, before Origin Rules, so when the Origin Rule is evaluated http.request.uri.path already holds the rewritten path without /api. Match on raw.http.request.uri.path, which keeps the received value.

solid answer

~40 s

Cloudflare evaluates each Rules product in a fixed sequence of phases: Single Redirects, URL normalization, URL Rewrite Rules, Configuration Rules, Origin Rules, then the WAF, Bulk Redirects, header transforms, Cache Rules and Snippets. Field values are fixed within a phase but carry changes into later phases, so after the rewrite `http.request.uri.path` is `/orders` and `starts_with(http.request.uri.path, "/api/")` is false. Fix the Origin Rule to use `raw.http.request.uri.path`, which never changes during the request, or match the rewritten path deliberately. Then sweep every later rule that matches on that path, WAF custom rules included, and confirm with Cloudflare Trace. Reordering rules cannot help, because order only applies inside one phase.

code

plaintext · 8 lines
plaintext
URL Rewrite Rule, dynamic path:
  wildcard_replace(http.request.uri.path, "/api/*", "/${1}")

Origin Rule expression that stopped matching:
  starts_with(http.request.uri.path, "/api/")

Origin Rule expression that keeps matching:
  starts_with(raw.http.request.uri.path, "/api/")

go deeper

for a junior

Know that Cloudflare evaluates Rules products in a fixed order and that a rewrite changes the path later rules see.

for a middle

Explain field immutability within a phase versus changes carried between phases, and what the raw fields are for.

for a senior

Diagnose the broken rule from the phase list, fix it with a raw field, sweep later phases including WAF rules, and prove it with Cloudflare Trace.

for a principal

Set a team rule that any URL rewrite ships with a review of every later-phase expression on the same paths, security rules first.

## The symptom A team strips a prefix with a URL Rewrite Rule, turning `/api/orders` into `/orders`, because the backend never expected `/api`. On the same day an Origin Rule that sends `/api/` traffic to the API's port, written as `starts_with(http.request.uri.path, "/api/")`, stops matching, and API requests land on the web server's port. Neither rule is wrong on its own; the problem is the order in which Cloudflare evaluates them and what each one sees. ## Phases run in a fixed order The **Ruleset Engine** evaluates each Rules product in its own **phase**, and the request phases run in a fixed order. The ones that matter here: 1. `http_request_dynamic_redirect`: Single Redirects 2. `http_request_sanitize`: URL normalization 3. `http_request_transform`: URL Rewrite Rules 4. `http_config_settings`: Configuration Rules 5. `http_request_origin`: Origin Rules 6. WAF custom rules, rate limiting rules and the WAF managed rules 7. `http_request_redirect`: Bulk Redirects 8. Managed Transforms, then `http_request_late_transform`: Request Header Transform Rules 9. `http_request_cache_settings`: Cache Rules 10. `http_request_snippets`: Snippets Reordering rules in the dashboard changes the order only inside one product's phase. No setting moves Origin Rules ahead of URL Rewrite Rules. ## Fields: fixed within a phase, updated between phases Two documented behaviours explain the whole incident: - **Within a phase, field values are immutable.** Every URL Rewrite Rule evaluates its expression against the path as it entered the phase, so a second rewrite rule cannot match on the first one's result. - **Between phases, fields carry the change.** After `http_request_transform`, the `http.request.uri`, `http.request.uri.path` and `http.request.full_uri` fields hold the rewritten values. The Origin Rule now sees `/orders`, so `starts_with(http.request.uri.path, "/api/")` is false. The **raw fields**, such as `raw.http.request.uri.path` and `raw.http.request.full_uri`, keep the values Cloudflare received for the whole request and do not change in any phase. ## Fixing it - Rewrite the later rule on the raw field: `starts_with(raw.http.request.uri.path, "/api/")`. Cloudflare's own tutorial that pairs a URL rewrite with an Origin Rule matches both on `raw.http.request.uri.path` for exactly this reason. - Or write the later rule against the rewritten path, if the rewritten form is what it really means to select. - Sweep every rule in a later phase that matches on a path the rewrite changes: Configuration Rules, WAF custom rules and rate limiting rules, Bulk Redirect Rules keyed on `http.request.full_uri`, Cache Rules and snippet rules. A WAF custom rule guarding `/api/admin` has the same blind spot, and that one matters more than a misrouted request. - Remember what raw fields skip: they hold the URL before URL normalization, so a security rule on a raw field must cope with encodings such as `%68ello` that normalization would have folded into `hello`. ## Confirming it **Cloudflare Trace** simulates a request through the zone and lists which rules matched in each product, so you can watch the URL Rewrite Rule fire and the Origin Rule not. It needs an Administrator or Super Administrator role and tests what-if requests; Log Explorer is the tool for traffic that actually happened. A useful habit after any change to a URL Rewrite Rule: 1. Trace a request for each path the rewrite touches. 2. Check that every later product still matches what it should. 3. Only then let the change take production traffic. ## The same model inside one phase Within the URL rewrite phase itself, the opposite surprise appears: two rewrite rules that chain, `/old/` to `/new/` and then `/new/` to something else, do not chain, because the second rule still sees `/old/`. Both surprises come from the one model: each phase works on a snapshot taken when it starts, and the next phase gets the result. ## A convention that prevents the next incident The fix for one rule is easy; keeping the zone consistent is the harder part. Pick one convention per zone and write it down: - **Public paths everywhere.** Every rule after the rewrite phase matches on raw fields, so authors always write the path a visitor types. This reads naturally, but raw fields skip normalization, so security rules need care with encodings. - **Internal paths after the rewrite.** Later rules match on the rewritten path, the one the origin sees. This keeps normalization, but every author must know which rewrites exist. Either works; a zone that mixes both is where the next silent mismatch comes from. Put the chosen convention in the review checklist for every URL Rewrite Rule change.

  • Two URL Rewrite Rules: the first rewrites /old/ to /new/, the second matches /new/ and adds a query string. Why does the second never fire on /old/ requests?
    Both rules run in the same phase, and field values are immutable within a phase, so the second rule's expression still sees /old/. Put both changes in one rule, or write the second rule's expression on the original path.
  • The WAF custom rule protecting /api/admin stopped blocking after the rewrite. Is it the same cause?
    Yes. WAF custom rules run in a phase after URL rewriting, so they see the rewritten path. Rewrite the rule against the new path or the raw field, and review every security rule whenever a rewrite changes paths.
  • How do you confirm which rules matched in each phase without waiting for production traffic?
    Run Cloudflare Trace for the URL. It simulates the request through the zone and shows the rules that matched in each product, Snippets included. It needs an Administrator or Super Administrator role; Log Explorer covers real historical traffic instead.

  • A URL Rewrite Rule only changes what the origin sees, so later Cloudflare rules still match the visitor's path.
  • Later rules in the same phase see the changes earlier rules made.
  • raw.http.request.uri.path holds the rewritten path in later phases.
  • Origin Rules run before URL Rewrite Rules.
  • Moving the Origin Rule higher in the dashboard list makes it see the original path.