I'm using a PHP script along with some .htaccess
rules for cache busting for automatically versioning files on a static website.
The source of the script is here: http://www.particletree.com/notebook/automatically-version-your-css-and-javascript-files/
I changed the names of variables and functions, but I don't think that's the problem.
Script in header.php:
<?php
function VERSIONING($url){
$path = pathinfo($url);
$ver = '.'.filemtime($_SERVER['DOCUMENT_ROOT'].$url).'.';
echo $path['dirname'].'/'.str_replace('.', $ver, $path['basename']);
}
?>
using the function on script, css, etc. in header.php:
<?php VERSIONING('/resources/css/stylesheet.css'); ?>
<?php VERSIONING('/resources/js/script.js'); ?>
<?php VERSIONING('/resources/img/logo.png'); ?>
.htaccess rules for cache busting:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(js|css|jpe?g|png|gif|ico)$ $1.$3 [L]
</IfModule>
Now here's the problem. It works just fine, but every 10-24 or so hours, I receive errors in my error_log
file for each file I'm using VERSIONING
on.
Each error looks like this:
[16-Mar-2013 09:06:24] PHP Warning: filemtime(): stat failed for /usr/local/apache/htdocs/resources/js/script.js in /home5/username/public_html/resources/header.php on line 42
And line 42 contains the following:
$ver = '.'.filemtime($_SERVER['DOCUMENT_ROOT'].$url).'.';
I have no idea what the problem could be. My only guess is that the DOCUMENT_ROOT is being interpreted differently sometimes, and failing to properly locate the files during versioning.
Any ideas?