是否有任何方法可以在CDN(不是Cloudfront,在本例中为Edgecast)中为js和css文件实现类似的版本控制解决方案,因为它是相当整洁的,结合了Rewrite规则和PHP,如this thread所述?我不知道如何让PHP / mod-rewrite组合在CDN上运行,经常更改我的版本,并且不想手动进行版本控制.我使用无cookie,完全独立的域来提供静态内容,因此我必须在函数中指定完整的URL.
为方便起见,我将在此处列出其他线程的代码.
首先,我们在.htaccess中使用以下重写规则:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s # Make the file doesn't actually exist
RewriteRule ^(.*)\.[\d]+\.(css|js)$$1.$2 [L] # Strip out the version number
/**
* 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);
}
现在,无论我们在哪里加入CSS,我们都会改变它:
<link rel="stylesheet" href="/css/base.css" type="text/css" />
对此:
<link rel="stylesheet" href="<?=auto_version('/css/base.css')?>" type="text/css" />
这将呈现为此类内容,确保始终提供最新版本,而无需手动更新版本:
<link rel="stylesheet" href="/css/base.1251992914.css" type="text/css" />
为了让这个在外部CDN(在一个完全不同的域上)工作,我试图替换
<link rel="stylesheet" href="<?=auto_version('/css/base.css')?>" type="text/css" />
通过这样的东西……
<link rel="stylesheet" href="<?='http://cdn.externaldomain.com' . auto_version('/css/base.css')?>" type="text/css" />
解决方法:
事实证明我的解决方案:
<link rel="stylesheet" href="<?= 'http://cdn.externaldomain.com' . auto_version('/css/base.css') ?>" type="text/css" />
作品.我只是错过了代码中的空格.