Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Created October 7, 2013 23:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clouddueling/6877043 to your computer and use it in GitHub Desktop.
Save clouddueling/6877043 to your computer and use it in GitHub Desktop.
Auto version js and css
<IfModule mod_rewrite.c>
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]
</IfModule>
/**
* 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).
*/
public static function auto_version($file)
{
$file = strpos($file, '/') === 0 ? $file : "/{$file}";
if(!file_exists($_SERVER['DOCUMENT_ROOT'] . $file))
return $file;
$mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment