Cloudflare not caching images served by PHP?


So I’ve run into a problem. The CMS I use allows specification width and height values in the url for an image and if you do it htaccess redirects to a php file which serves the resized version of the image instead of the normal sized one. (It is an internal redirect which serves proper image headers, as far as I know)

Only problem is that cloudflare doesn’t seem to cache these images and I’m not sure why.

Take the following images as an example. The first one is a url directly to the image in question. The second one is a url to a resized version of the image by PHP. Note how much slower the second one loads as cloudflare doesn’t seem to cache it. (even though the file size is far smaller)

I’m grateful for any direction. Backend is not my area of expertise. :wink:



They both have respective caching headers in their responses.



No, it won’t do by default.

:warning: PHP are supposed to be dynamic, I would personally avoid that approach for serving images as it can create cache conflict. If that is WordPress, Photon may be worth trying instead of using PHP dynamic serving.

Still want to give a try? Here you go!

  • Login to Cloudflare
  • Select domain
  • Go to page rule, create a new rule

Page rule conditions

  • Edge cache: 1 month or any value (mandatory)
  • Browser cache: 4 hours or any value

  • Deploy and test in various screen resolution, devices.

Possible problem, unless it changes query string or file URL, there may be potential cache conflict. You have been warned! :grinning:

What does above page rule?

No matter what cache control is defined (even cache-control: no-cache), just do cache. It’s aggressive mode.