Set up rewrite rules
Last updated Apr 21, 2026 You can use Transform Rules to rewrite URLs for every image that you transform through Images.
This page covers examples for the following scenarios:
- Serve images from custom paths
- Modify existing URLs to be compatible with transformations in Images
- Transform every image requested on your zone with Images
To create a rule:
- In the Cloudflare dashboard, go to the Rules Overview page. Go to Overview ↗
- Select Create rule next to URL Rewrite Rules.
Before you start
Every rule runs before and after the transformation request.
If the path for the request matches the path where the original images are stored on your server, this may cause the request to fetch the original image to loop.
To direct the request to the origin server, you can check for the string image-resizing in the Via header:
...and (not (any(http.request.headers["via"][*] contains "image-resizing")))
Serve images from custom paths
By default, requests to transform images through Images are served from the /cdn-cgi/image/ path. You can use Transform Rules to rewrite URLs.
Basic version
Free and Pro plans support string matching rules (including wildcard operations) that do not require regular expressions.
This example lets you rewrite a request from example.com/images to example.com/cdn-cgi/image/:
Text in Expression Editortxt
(starts_with(http.request.uri.path, "/images")) and (not (any(http.request.headers["via"][*] contains "image-resizing")))
Text in Path > Rewrite to > Dynamictxt
concat("/cdn-cgi/image", substring(http.request.uri.path, 7))
Advanced version
Note
This feature requires a Business or Enterprise plan to enable regex in Transform Rules. Refer to Cloudflare Transform Rules Availability for more information.
There is an advanced version of Transform Rules supporting regular expressions.
This example lets you rewrite a request from example.com/images to example.com/cdn-cgi/image/:
Text in Expression Editortxt
(http.request.uri.path matches "^/images/.*$") and (not (any(http.request.headers["via"][*] contains "image-resizing")))
Text in Path > Rewrite to > Dynamictxt
regex_replace(http.request.uri.path, "^/images/", "/cdn-cgi/image/")
Modify existing URLs to be compatible with transformations in Images
Note
This feature requires a Business or Enterprise plan to enable regex in Transform Rules. Refer to Cloudflare Transform Rules Availability for more information.
This example lets you rewrite your URL parameters to be compatible with Images:
(http.request.uri matches "^/(.*)\\?width=([0-9]+)&height=([0-9]+)$")
Text in Path > Rewrite to > Dynamictxt
regex_replace(
http.request.uri,
"^/(.*)\\?width=([0-9]+)&height=([0-9]+)$",
"/cdn-cgi/image/width=${2},height=${3}/${1}"
)
Leave the Query > Rewrite to > Static field empty.
Pass every image requested on your zone through Images
Note
This feature requires a Business or Enterprise plan to enable regular expressions in Transform Rules. Refer to Cloudflare Transform Rules Availability for more information.
This example lets you transform every image that is requested on your zone with the format=auto option:
(http.request.uri.path.extension matches "(jpg)|(jpeg)|(png)|(gif)") and (not (any(http.request.headers["via"][*] contains "image-resizing")))
Text in Path > Rewrite to > Dynamictxt
regex_replace(http.request.uri.path, "/(.*)", "/cdn-cgi/image/format=auto/${1}")
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/images/optimization/transformations/rewrite-rules/#page","headline":"Set up rewrite rules","description":"Use Transform Rules to rewrite URLs for Cloudflare Images transformations and serve images from custom paths.","url":"https://developers.cloudflare.com/images/optimization/transformations/rewrite-rules/","inLanguage":"en","image":"https://developers.cloudflare.com/images/optimization/transformations/rewrite-rules/og.png?v=3ba0e661175be41d","dateModified":"2026-04-21","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Manage which origins Cloudflare Images can use as the source for image transformations. title: Define source origins image: https://developers.cloudflare.com/images/optimization/transformations/sources/og.png?v=7a75418215992362
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/images/llms.txt
Use this file to discover all available pages before exploring further.
Docs