I have a PHP application running on an Apache2 server. I started off with HTML5 Boilerplate, and the .htaccess
includes "1 month" expiry policy for all images. For example:
ExpiresByType image/jpeg "access plus 1 month"
My application also allows a user to replace an existing image with a new one (under the same name and path). However, once replaced, the user continues to see the old image until the page is force-refreshed (say, Ctrl-F5).
I noticed from the logs that technically, since the image is set to expire 1 month from now, the browser doesn't ask the server for it. Only on a force refresh does the browser ask for it, at which point Apache2 may issue a 304
(if the file never changed) or send the new image with a 200
(if it changed).
What I want is a simple straightforward mechanism to:
- Keep the existing setup where the images expire 1 month from now. Browsers must not need to fetch it if they already have it.
- But if the image is updated by the user, automatically have the browser fetch the new image. There should be no need to tell the user to force a refresh.
- The fetching of new image must also extend to other users or other browser after the last point.
As an alternative, I wouldn't mind if I can conditionally set the expiry policy to "immediate" for a particular path only (images in other folders must follow the general expiry policy). I can live with the numerous 304
s. I guess.
I don't want to simply change the filename to a new one. The filenames are dynamically generated, and there are several - tracking their "new names" would add complexity. (Although if there's no other easy solution, I'll have to explore this.)