The following origin rule overrides the HTTP Host header to hr-server.example.com for all requests with a URI path starting with /hr-app/. It also overrides the DNS record to the same hostname.
The Host header override only updates the header value; the DNS record override will handle the rerouting of incoming requests. For more information on these overrides, refer to Origin Rules settings.
Expression when using the Expression Builder:
| Field | Operator | Value |
|---|---|---|
| URI Path | starts with | /hr-app/ |
Expression when using the Expression Editor:
(starts_with(http.request.uri.path, "/hr-app/"))Value after Host Header > Rewrite to:
hr-server.example.comValue after DNS Record > Override to:
hr-server.example.comThe following example sets the rules of an existing phase ruleset ($RULESET_ID) to a single origin rule — overriding the Host header of incoming requests and the resolved DNS record — using the Update a zone ruleset operation. The response will contain the complete definition of the ruleset you updated.
Required API token permissions
At least one of the following token permissions
is required:
Response Compression WriteConfig Settings WriteDynamic URL Redirects WriteCache Settings WriteCustom Errors WriteOrigin WriteManaged headers WriteZone Transform Rules WriteMass URL Redirects WriteMagic Firewall WriteL4 DDoS Managed Ruleset WriteHTTP DDoS Managed Ruleset WriteSanitize WriteTransform Rules WriteSelect Configuration WriteBot Management WriteZone WAF WriteAccount WAF WriteAccount Rulesets WriteLogs WriteLogs Write
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
--request PUT \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"rules": [
{
"ref": "hr_app_overrides",
"expression": "starts_with(http.request.uri.path, \"/hr-app/\")",
"description": "Origin rule for the company HR application",
"action": "route",
"action_parameters": {
"host_header": "hr-server.example.com",
"origin": {
"host": "hr-server.example.com"
}
}
}
]
}'{
"result": {
"id": "<RULESET_ID>",
"name": "Origin Rules ruleset",
"description": "Zone-level ruleset that will execute origin rules.",
"kind": "zone",
"version": "2",
"rules": [
{
"ref": "hr_app_overrides",
"id": "<RULE_ID>",
"version": "1",
"action": "route",
"action_parameters": {
"host_header": "hr-server.example.com",
"origin": {
"host": "hr-server.example.com"
}
},
"expression": "starts_with(http.request.uri.path, \"/hr-app/\")",
"description": "Origin rule for the company HR application",
"last_updated": "2022-06-03T14:42:04.219025Z",
"ref": "<RULE_REF>"
}
],
"last_updated": "2022-06-03T14:42:04.219025Z",
"phase": "http_request_origin"
},
"success": true,
"errors": [],
"messages": []
}Use the ref field to get stable rule IDs across updates when using Terraform. Adding this field prevents Terraform from recreating the rule on changes. For more information, refer to Troubleshooting in the Terraform documentation.