ExpiresActive On
ExpiresDefault "access plus 10 years"
把這些檔案存取 10年. 除非他按下 ctrl + F5 重新整理.
方法二:
.htaccess
#Rules for Versioned Static Files
RewriteRule ^(scripts|css)/(.+)\.(.+)\.(js|css)$ $1/$2.$4 [L]
會將 css 與 js 檔案 從 js/jquery.123.js ==> js/jquery.js
也可以使用在圖檔
#Rules for Versioned Static Files
RewriteRule ^(scripts|css|images)/(.+)\.(.+)\.(js|css|jpg|gif|png)$ $1/$2.$4 [L]
搭配php function
function autoVer($url){
$path = pathinfo($url);
$ver = '.'.filemtime($_SERVER['DOCUMENT_ROOT'].$url).'.';
echo $path['dirname'].'/'.str_replace('.', $ver, $path['basename']);
}
include($_SERVER['DOCUMENT_ROOT'].'/path/to/autoVer.php');
<link rel="stylesheet" href="<?php autoVer('/css/structure.css'); ?>" type="text/css" />
<script type="text/javascript" src="<?php autoVer('/scripts/prototype.js'); ?>"></script>
結果變成:
<link rel="stylesheet" href="/css/structure.1194900443.css" type="text/css" />
<script type="text/javascript" src="/scripts/prototype.1197993206.js"></script>
出處:http://particletree.com/notebook/automatically-version-your-css-and-javascript-files/
沒有留言:
張貼留言