Cache issue with WEBP fallback images?


Many of the sites we have on CloudFlare (FREE plan) serve WEBP images which fallback to their original format if WEBP is not supported. This is done using the following htaccess rules:

RewriteCond %{HTTP_ACCEPT} !image/webp [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1$2$3/$4.jpg -f
RewriteRule ^(.*?)(site/assets/files/)([0-9]+)/(.*)\.webp(.*)$ /$1$2$3/$4.jpg [R=307,L]

RewriteCond %{HTTP_ACCEPT} !image/webp [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1$2$3/$4.jpeg -f
RewriteRule ^(.*?)(site/assets/files/)([0-9]+)/(.*)\.webp(.*)$ /$1$2$3/$4.jpeg [R=307,L]

RewriteCond %{HTTP_ACCEPT} !image/webp [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1$2$3/$4.png -f
RewriteRule ^(.*?)(site/assets/files/)([0-9]+)/(.*)\.webp(.*)$ /$1$2$3/$4.png [R=307,L]

I added a new site yesterday and it was working fine. Indeed, I’ve not noticed an issue before with any sites, some which have been using WEBP for months.

However, this morning when I checked the new site, none of the images were loading for browsers that do not support WEBP. I found that if I appended a cache buster string (e.g. ?nc=12345678), the fallback image was served correctly, but the cached one was not working. Purging the cache for the site resolved the problem - temporarily. The corrupted/missing images have returned after a few hours (probably 4 as per setting).

Why would this suddenly just stop working? Could it be related to the Network Performance issues on 23/4/20?

What could be causing it? I haven’t changed any settings from when it was working to now. Perhaps a change on CloudFlare’s end is stopping the htaccess redirect/fallback from working now?


Chris Thomson

I’m surprised it was fine before. As soon as Cloudflare caches a filename, that request will no longer hit your server and your .htaccess will be ignored.

So it turns out that only one other site was actually using WEBP, so you may be right that it hasn’t been working until now on that one, and it is only with the new site that I’ve noticed it :confused:

1 Like

Going to investigate further next week…

1 Like

That is because Cloudflare cache ignores and doesn’t support HTTP Vary header. So if a webP browser supported visitor populates the CF cache with a .png or .jpg extension file with webP mime type from your origin web server’s webP support, that cached file will be served to non-webP browser i.e. Safari on subsequent visits by another visitor.

I posted a product request to add HTTP Vary header support for CF cache at Cloudflare CDN Cache To Support HTTP Vary Header which you can vote on. I also posted how I worked around it on Nginx origin to disable origin side webP if Cloudflare proxy is detected on a domain. Not sure of the Apache equivalent - haven’t used Apache in years now heh.

1 Like

Thanks, have voted!

This topic was automatically closed after 30 days. New replies are no longer allowed.