URL Rewrite Rules and Origin Rules: URI Path, Host Header, and SNI Overrides
How Cloudflare URL Rewrite Rules and Origin Rules change paths, query strings, Host headers, DNS targets, SNI, and ports—and how to combine them safely.
This page summarises Cloudflare documentation on URL Rewrite Rules (part of Transform Rules) and Origin Rules. Together they let you keep the URL visitors see in the browser while changing what reaches your origin—path segments, query strings, which server receives the request, and how TLS presents the hostname.
URL Rewrite Rules: change path and query, not hostname
Section titled “URL Rewrite Rules: change path and query, not hostname”A URL rewrite is a server-side change from a source URL to a target URL before your origin fully handles the request. Visitors do not see the browser URL change—that behaviour belongs to redirects, not rewrites.
You can manipulate the URL of a request through different operations, namely rewrites and redirects:
URL rewrite: A server-side operation that converts a source URL into a target URL. It occurs before a web server has fully processed a request. A rewrite is not visible to website visitors, since the URL displayed in the browser does not change.
— Cloudflare, URL Rewrite Rules
URL Rewrite Rules can rewrite the URI path, the query string, or both. They come in two forms:
| Type | What it does | Typical use |
|---|---|---|
| Static rewrite | Replaces path or query with a fixed string | Country-specific welcome file, append ?sort-by=date |
| Dynamic rewrite | Uses a rewrite expression built from request fields | Strip a path prefix with regex_replace(), wildcard capture with ${1} |
The combined length of all rewrite parameter values is limited to 4,096 characters (path plus query together). See URL rewrite parameters.
For hostname or backend changes, use Origin Rules (below). For logic beyond built-in rules, Cloudflare points to Snippets in the same product family.
Origin Rules: route traffic and override Host, SNI, DNS, and port
Section titled “Origin Rules: route traffic and override Host, SNI, DNS, and port”Origin Rules change where Cloudflare sends a request or how the connection is established. Each rule applies only to traffic matching its filter expression (hostname, path, and other request properties).
Origin Rules allow you to change the origin server that Cloudflare sends a request to, or modify how the request reaches that server. This is useful when you need to route specific requests to a different backend, such as a third-party service or a server on a non-standard port. You can perform the following overrides:
Host header: Overrides the Host header of incoming requests. Server Name Indication (SNI): Overrides the Server Name Indication (SNI) value of incoming requests. DNS record: Overrides the resolved hostname of incoming requests, sending the request to a different origin server. Destination port: Overrides the resolved destination port of incoming requests.
— Cloudflare, Origin Rules
Origin Rules require proxied DNS (orange-cloud) for the zone or subdomain they apply to.
In practice, DNS record override (resolve override) picks the target IP or hostname Cloudflare connects to, while Host header override tells the application which site the request is for. Documentation stresses pairing them for third-party backends:
In most situations, when you rewrite the HTTP Host header you also need to configure a DNS record override. The Host header override only updates the header value; the DNS record override will handle the rerouting of the request.
— Cloudflare, Origin Rules settings — Host header
The reverse also applies when only DNS is overridden: you usually need a matching Host header override so the origin accepts the request. See DNS record in the same reference.
Host header values can include a port (for example example.com:1234) per the rules changelog. API shape for combined overrides is documented in Origin Rules API parameter reference.
Host header override and SNI on the origin connection
Section titled “Host header override and SNI on the origin connection”When an origin rule sets the Host header, Cloudflare typically sets SNI on the outbound TLS connection to the same value. To use a different SNI, add an SNI override in the same origin rule or a separate one:
Like Page Rules, an origin rule performing a Host header override will update the SNI value of the original request to the same value of the Host header. To set an SNI value different from the Host header override, add an SNI override in the same origin rule or create a separate origin rule for this purpose.
— Cloudflare, Origin Rules
SNI override values must be valid hostnames on the same Cloudflare account; only static SNI values are supported in Origin Rules. An SNI override takes precedence over SNI rewrites on custom origins in Cloudflare for SaaS. Connection request details describe fallback vs custom origin SNI behaviour at the platform level.
Rule evaluation order: URL rewrite before origin override
Section titled “Rule evaluation order: URL rewrite before origin override”For the common pattern “public URL under /uploads/* → different host and stripped path”, both rule types must use tight, matching filters.
Remember that rules are evaluated in sequence, so Transform Rules (including URL rewrites) run before Origin Rules or Cloud Connector.
— Cloudflare, Change URI path and Host header
Ensure filters for the URL rewrite and the origin rule (or Cloud Connector rule) are precise to avoid unintended rule executions.
For object storage buckets, the change URI path and Host header tutorial recommends Cloud Connector instead of an origin rule when routing to a bucket.
Combine URL rewrite with Origin Rule: path strip plus new backend
Section titled “Combine URL rewrite with Origin Rule: path strip plus new backend”The Change URI path and Host header tutorial routes https://<YOUR_SOURCE_HOSTNAME>/uploads/* to https://<YOUR_TARGET_HOSTNAME>/* by:
- A URL Rewrite Rule with expression
raw.http.request.uri.path matches "^/uploads/.*"and dynamic path rewriteregex_replace(raw.http.request.uri.path, "^/uploads/", "/"). - An Origin Rule with the same path match, Host Header > Rewrite to your target hostname, and optional DNS Record > Override to that hostname.
The same path prefix removal + origin rule pattern appears in Point to Pages with a custom domain (/blog/* → ${1}) and Point to R2 bucket with a custom domain (/images/* → ${1}), including the dashboard template Rewrite Path for Object Storage Bucket.
Rewrite expressions: allowed fields and functions
Section titled “Rewrite expressions: allowed fields and functions”Dynamic URL rewrites are limited to specific request data in the rewrite expression (not necessarily the same as the rule’s match expression):
A rewrite expression (that is, the expression that defines the dynamic URL rewrite to perform) can only include the following fields:
http.request.uri.* http.request.headers.* http.request.accepted_languages
The concat(), regex_replace(), and wildcard_replace() functions can appear only once in a rewrite expression. Additionally, you cannot nest the regex_replace() and wildcard_replace() functions.
— Cloudflare, Available fields and functions
regex_replace() for path patterns
Section titled “regex_replace() for path patterns”regex_replace() is case-sensitive by default, replaces only the first match, supports capture groups ${1}…${8}, and escapes $ in the replacement as $$. It is available in URL rewrite rewrite expressions and dynamic redirect targets—not nested with wildcard_replace() in the same expression. Full semantics and examples are in regex replace.
Example from Rewrite blog archive URLs:
- Match:
http.request.uri.path ~ "^/posts/[0-9]+-[0-9]+-[0-9]+-.*" - Rewrite:
regex_replace(http.request.uri.path, "^/posts/([0-9]+)-([0-9]+)-([0-9]+)-(.*)$", "/posts/${1}/${2}/${3}/${4}")
wildcard_replace() and dashboard wildcard UI
Section titled “wildcard_replace() and dashboard wildcard UI”The dashboard Wildcard pattern flow maps to wildcard_replace() on path and/or query; capture groups use ${1}, ${2}, etc. The Request URL wildcard only matches traffic—it does not capture segments for the rewrite. Target path and target query must align with what you want to replace. See Wildcard pattern parameters and URL rewrite parameters — Wildcard matching.
Create and deploy rules
Section titled “Create and deploy rules”URL Rewrite Rule in the dashboard
Section titled “URL Rewrite Rule in the dashboard”From Rules > Overview, choose Create rule > URL Rewrite Rule. Match traffic via wildcard pattern, custom filter expression, or all requests. For each of path and query, choose Rewrite to (static or dynamic), or Preserve. Place the rule in the list order you need, then Deploy or Save as Draft. Details: Create a URL rewrite rule in the dashboard.
Origin Rule in the dashboard
Section titled “Origin Rule in the dashboard”Same Rules Overview entry point: Create rule > Origin Rule, define When incoming requests match, then set origin parameters (Host, SNI, DNS, port). See Create an origin rule in the dashboard.
API and Terraform
Section titled “API and Terraform”| Rule type | Phase | Action | Token permissions (minimum) |
|---|---|---|---|
| URL rewrite | http_request_transform |
rewrite |
Account Transform Rules > Edit, Account Rulesets > Read |
| Origin rule | http_request_origin |
route |
Zone Origin Rules > Edit |
Workflow: list zone rulesets, create the phase ruleset if missing, update ruleset rules. See Create a URL rewrite rule via API and Create an origin rule via API. Terraform examples: Transform Rules configuration and Define a single origin rule using Terraform.
When several rules match
Section titled “When several rules match”Origin Rules: If two or more origin rules match, Cloudflare merges configurations; later rules override parameters from earlier rules on conflicts (for example port from rule #1, Host and DNS host from rule #2). See Origin Rules FAQ.
Origin Rules vs Page Rules: If both set Host or DNS overrides, origin rule fields win where both define the same parameter; mixed cases can leave Host from a Page Rule and DNS from an Origin Rule. Same FAQ.
Load balancer Host override: An origin rule overrides load balancer Host header configuration when both apply. Origin Rules also state origin rules take precedence over load balancer header overrides when hostname is overridden via Host or DNS.
Workers: When a request is handled by a Worker, Host header override on an origin rule is not applied when evaluating Workers routes (Host header settings).
Cloud providers (SaaS): If Azure App Service, AWS ALB, or GCP Cloud Run returns 404 because the Host does not match their configured domain, either register the custom hostname on the provider or use an Origin Rule to set the Host header they expect—described under Custom origin server.
Common mistakes
Section titled “Common mistakes”- Rewriting hostname in a URL Rewrite Rule — use an Origin Rule (or Cloud Connector for buckets).
- DNS override without Host override (or the reverse) on third-party origins — connection reaches the server but the application rejects the request.
- Assuming Request URL wildcards capture path segments — capture happens in Target path / Target query rewrite fields only (Wildcard pattern parameters).
- Nested or duplicate
regex_replace()/wildcard_replace()in one rewrite expression — not allowed (fields and functions). - Using
http.request.full_uriwith a path-only wildcard pattern —wildcard_replace()may leave the value unchanged; usehttp.request.uri.pathwhen rewriting paths (wildcard replace). - Broad rule expressions — unrelated traffic hits rewrite or origin overrides; mirror tutorial-style path anchors like
^/uploads/.*. - Expecting Full (strict) TLS when custom hostname Host and custom-origin SNI disagree — may need SNI rewrite entitlements or an Origin Rule SNI override (Custom origin server — SNI rewrites).
How to check this on your site
Section titled “How to check this on your site”-
In the Cloudflare dashboard, open Rules > Overview and confirm URL Rewrite Rules and Origin Rules are Deployed, ordered so Transform Rules run before Origin Rules as described in Change URI path and Host header.
-
Use Cloudflare Trace (referenced from URL rewrite dashboard docs) to verify that matching requests hit the expected rewrite rule.
-
Request a URL that should match your filters (for example a path under
/uploads/or your wildcard prefix) and confirm the browser URL is unchanged while the origin receives the rewritten path and Host you configured. -
On the origin or backend logs, verify the Host header and target host/port match your Origin Rule (and that TLS/SNI errors disappear if you added an SNI override).
-
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact wording you are asking about, and we will correct or clarify it.