Skip to content

Change URI Path and Host Header

Modify both the URI path and Host header of incoming requests using Transform Rules and Origin Rules.

To change URI Path and Host Header on incoming requests simultaneously:

  1. Log in to the Cloudflare dashboard and select your account and domain.
  2. Create a Transform Rule:
    • Navigate to Rules > Transform Rules.
    • Click on the Rewrite URL tab and select Create rule.
    • Configure the rule to modify the path. For instance, to remove /uploads from the path:

Text in Expression Editor:

raw.http.request.uri.path matches "^/uploads/.*"

Text after Path > Rewrite to… > Dynamic:

regex_replace(raw.http.request.uri.path, "^/uploads/", "/")

The regex_replace() function replaces the /uploads/ part of the path with /, changing /uploads/example.jpg to /example.jpg.

  1. Create an Origin Rule:
    • If routing traffic to an object storage bucket, use Cloud Connector. Otherwise, navigate to Rules > Origin Rules.
    • Click Create rule and select Host Header > Rewrite to…
    • Configure the rule to modify the Host header to desired hostname:

Text in Expression Editor:

raw.http.request.uri.path matches "^/uploads/.*"

Text after Host Header > Rewrite to…:

example.com

This will set the Host header to example.com for matching requests.

Optional: To route requests to a different origin (DNS target), use DNS override:

Text after DNS Record > Override to…:

example.com

This will route requests to the DNS target of example.com instead of your default DNS record.

This setup routes traffic from https://<YOUR_SOURCE_HOSTNAME>/uploads/* to https://<YOUR_TARGET_HOSTNAME>/*. Ensure the filters for Transform Rules and Origin Rules (or Cloud Connector) are precise to avoid unintended rule executions.

Remember that rules are evaluated in sequence, so Transform Rules run before Origin Rules or Cloud Connector.

Make sure to replace example.com with your actual hostname and adjust the example paths according to your setup. By following these steps, you can effectively manage both URI paths and Host headers to route traffic appropriately and optimize request handling.