PHP: I’m trying to determine the best way to prevent caching of a CSS file when it is changed. I’m working with IE6/7/8/9, Firefox, Chrome, iPAD’s and some other browsers not mentioned. Code is PHP with CodeIgniter. I was working on a mod rewrite rule to provide a modified path; something like /timestamp/file.css. But, then I noticed posts saying that you could just add a workstring to the CSS file and the browser will see it as a new file – as long as the workstring portion has changed. Will this workstring method work for all browsers? Something like file.css?v=1234567890. If v changes the browser should recache? Can I trust this method?
<?php
echo '<link rel="stylesheet" type="text/css" href="file.css?v=' . filemtime('file.css') . '" />';
?>
Yes, it should. A different query string is an entirely separate URL as far as a browser is concerned.