-
-
Save Lovor01/43b45759a69bfa96a5a7b0a9b79d19ac to your computer and use it in GitHub Desktop.
PHP CSS&JS auto-versioning function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CSS/JS auto-versioning | |
RewriteEngine On | |
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 string $file The file to be loaded. Must be an absolute path (i.e. | |
* starts with slash). | |
* @return string | |
*/ | |
function auto_version($file) { | |
$fileorig = $file; | |
$file = str_replace('http://', '', $file); | |
$file = str_replace('https://', '', $file); | |
$file = str_replace($_SERVER['SERVER_NAME'], '', $file); | |
if ((strpos($file, '/') !== 0) && ($fileorig == $file)) | |
$file = '/' . $file; | |
$full_file = $_SERVER['DOCUMENT_ROOT'] . $file; | |
if (strpos($file, '/') !== 0 || !file_exists($full_file)) { | |
$full_file = substr($_SERVER['SCRIPT_FILENAME'], 0, -strlen($_SERVER['SCRIPT_NAME'])); | |
$full_file .= $file; | |
if (!file_exists($full_file)) { | |
return $file; | |
} | |
} | |
$mtime = filemtime($full_file); | |
$new_file = preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file); | |
return $new_file; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment