Serve images from custom paths
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, log in to the Cloudflare dashboard and select your account and website. Then, select Rules > Transform 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 only support string matching rules 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 Editor(starts_with(http.request.uri.path, "/images")) and (not (any(http.request.headers["via"][*] contains "image-resizing")))
Text in Path > Rewrite to... > Dynamicconcat("/cdn-cgi/image", substring(http.request.uri.path, 7))
Advanced version
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 Editor(http.request.uri.path matches "^/images/.*$") and (not (any(http.request.headers["via"][*] contains "image-resizing")))
Text in Path > Rewrite to... > Dynamicregex_replace(http.request.uri.path, "^/images/", "/cdn-cgi/image/")
Modify existing URLs to be compatible with transformations in Images
This example lets you rewrite your URL parameters to be compatible with Images:
Text in Expression Editor(http.request.uri matches "^/(.*)\\?width=([0-9]+)&height=([0-9]+)$")
Text in Path > Rewrite to... > Dynamicregex_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
This example lets you transform every image that is requested on your zone with the format=auto
option:
Text in Expression Editor(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... > Dynamicregex_replace(http.request.uri.path, "/(.*)", "/cdn-cgi/image/format=auto/${1}")