Serve images from custom domains
Image delivery is supported from all customer domains under the same Cloudflare account. To serve images through custom domains, an image URL should be adjusted to the following format:
Example with a custom domain:
In this example, <ACCOUNT_HASH>
, <IMAGE_ID>
and <VARIANT_NAME>
are the same, but the hostname and prefix path is different:
example.com
: Cloudflare proxied domain under the same account as the Cloudflare Images./cdn-cgi/imagedelivery
: Path to triggercdn-cgi
image proxy.ZWd9g1K7eljCn_KDTu_MWA
: The Images account hash. This can be found in the Cloudflare Images Dashboard.083eb7b2-5392-4565-b69e-aff66acddd00
: The image ID.public
: The variant name.
By default, Images are served from the /cdn-cgi/imagedelivery/
path. You can use Transform Rules to rewrite URLs and serve images from custom paths.
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/imagedelivery/<ACCOUNT_HASH>
.
To create a rule:
- Log in to the Cloudflare dashboard ↗ and select your account and website.
- Select Rules > Transform Rules.
- Select Create rule.
- Under When incoming requests match…, select Edit expression.
- In the text field, enter
starts_with(http.request.uri.path, "/images")
. - Under Path, select Rewrite to.
- Select Dynamic and enter the following in the text field.
- Select Deploy when you are done.
This example lets you rewrite a request from example.com/images/some-image-id/w100,h300
to example.com/cdn-cgi/imagedelivery/<ACCOUNT_HASH>/some-image-id/width=100,height=300
and assumes Flexible variants feature is turned on.
To create a rule:
- Log in to the Cloudflare dashboard ↗ and select your account and website.
- Select Rules > Transform Rules.
- Select Create rule.
- Under When incoming requests match…, select Edit expression.
- In the text field, enter
(http.request.uri.path matches "^/images/.*$")
. - Under Path, select Rewrite to.
- Select Dynamic and enter the following in the text field.
When using a custom domain, it is not possible to directly set up WAF rules that act on requests hitting the /cdn-cgi/imagedelivery/
path. If you need to set up WAF rules, you can use a Cloudflare Worker to access your images and a Route using your domain to execute the worker. For an example worker, refer to Serve private images using signed URL tokens.