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.
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")))
By default, requests to transform images through Images are served from the /cdn-cgi/image/
path.
You can use Transform Rules to rewrite URLs.
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/
:
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/
:
This example lets you rewrite your URL parameters to be compatible with Images:
Leave the Query > Rewrite to… > Static field empty.
This example lets you transform every image that is requested on your zone with the format=auto
option: