Custom sub domain for Cloudflare Images delivery?

I have a project I’ve thrown together: https://plantsm.art

Just last night I moved all my images to Cloudflare. The process was simple enough, but I would like to serve them on a more personalised sub domain, eg; https://cdn.plantsm.art instead of the provided delivery URL. I’m aware that I can use the cdn-cgi gateway path on my apex, but would prefer a dedicated sub domain. I’m also aware that I can’t create a CNAME and point it to the image delivery domain.

Is there a practical way to achieve what I’d like without running a reverse proxy or something?

Thank you in advance!

1 Like

The cdn-cgi/imagedelivery path will work on a subdomain automatically as well as on the apex.

1 Like

Thanks! It makes perfect sense this would work on a subdomain as well. Just didn’t make that mental leap. Apreesh!

Unfortunate that bulk redirect rules are $$$. I could combine the two to mask the cdn-cgi/imagedelivery/${account_id} stuff. This is a good enough solution for now.

:slight_smile:

So, you got me thinking, and I tried it and you can do this in one redirect rule.

Here, I’ve created a new hostname, foo, that doesn’t actually exist: it’s an AAAA record pointing to the special value 100:: with the :orange: orange cloud turned on.

I’m taking any request to that host where the path doesn’t already include imagedelivery (to avoid a redirect loop) and redirecting it to the /cdn-cgi/imagedelivery path, with my account hash appended since that will always be the same, and the rest of the path appended to that. It’s not all visible in the screenshot, but the Expression is:

concat("https://foo.example.com/cdn-cgi/imagedelivery/74PjBwfo8pUCTF5YoFotYg", http.request.uri.path)

(I’ve changed the domain name for this example, and you should use your own account hash, which is available on the Cloudflare Images page in the dashboard.)

Now any URLs like:

https://foo.example.com/a7c7982f-b5e5-4eac-76d8-0aaf773e3100/public

will redirect to the cdn-cgi version with your account hash on them. So the final URL is visible to the end-user if he looks (because it’s a real redirect) but it’s not in your HTML.

Yes, I tried it with a Transform Rule too, which seemed the cleanest way to do it, but it doesn’t like transforming a URL to /cdn-cgi, so I guess the rule comes in the processing chain too late to invisibly transform it.

5 Likes

Above and beyond, mate! This worked a treat!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.