So i found How to force the browser to reload cached CSS/JS files? question there, but i have troubles figuring out what i am doing wrong with php function written in the answers.
- <? php
- /**
- * Given a file, i.e. /css/base.css, replaces it with a string containing the
- * file's mtime, i.e. /css/base.1221534296.css.
- *
- * @param $file The file to be loaded. Must be an absolute path (i.e.
- * starting with slash).
- */
- function auto_version($file)
- {
- if(strpos($file, '/') !== 0 || !file_exists($_SERVER['DOCUMENT_ROOT'] . $file))
- return $file;
-
- $mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
- return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
- }
I tried to copy-paste all of this to my website, changed .htaccess, created php file, made a proper link to php echo main.css in my project, did everything like it was suggested in the answer. But my .css file failed to load, console showed error about my php query failure. My guess is the code above must be configured in some way, but i have almost none of the php knowledge and this part about @param $file makes me wonder, how must i write this? @param $file = /css/main.css or what does it mean? Or i need to place /css/main.css in place of every $file? I cant comment answer in that original thread, so created a new one
edit: structure of my site is this:
- index.html
- auto_version.php
- css/main.css
to index.html i added:
<link rel="stylesheet" href="<?php echo auto_version('/css/main.css'); ?>" type="text/css" />
I also edited first block of code with part, as it stands in mine .php file. So as you can see, index.html and auto_version.php both located in root folder of website, and main.css is inside of css folder. May this be an issue im not sure.