Cache Busting images inside CSS
My situation
I am currently using Cache Busting when I include these CSS files:
echo "<link href='stylesheet.css?" . filemtime('stylesheet.css') . "' />"
My goal
Now I would like to do something similar for images that I include in my CSS file.
Problem
The problem is that I cannot use PHP inside my CSS files, and I would prefer to store my CSS files separately.
My question
How can I add filemtime() to images inside my CSS files, saving the files separately?
Edit
I would like to use Far Future Expires headers to cache files.
In fact, you can rename your css files as style.css.php and then use PHP inside them. As long as the result of the subsequent processing is in the correct CSS format, it should work. I have done this in the past. I'm not sure if this is necessary, but if it gives you problems, you can use the header ("Content-type ...") and ensure that it is sent as a CSS file.
To get a cache overkill, the best way is to send the correct headers. Make sure Apache is configured to send the Expires: now header. So, in the .htaccss file:
Header always set Cache-Control "no-store, no-cache, must-revalidate" Header always set Expires "Thu, 01 Jan 1970 00:00:00 GMT" This will always be the reason for the lack of caching of all content in its directory and in any of them.
However, if you want to conditionally cache, what I would suggest, it does one of several things.
Include version number in CSS file name. This way you will have a file similar to
mycss.1.css,mycss.2.css. This requires a bit more work, since you need to coordinate both file names. But itโs better, since you donโt send files from PHP (you donโt get any resource), you can use CDN (even better), and you can still take advantage of the headers with a distant future.Set the
Cache-Control: must-revalidateand the corresponding E-Tag so that all he needs to do is send304 Not Modifiedif the content has not changed ...