另一种类似的方法是在提供文件时使用htaccess mod_rewrite 忽略部分路径。您从不缓存的索引页面引用了文件的最新路径。
从开发的角度来看,它就像使用参数作为版本号一样简单,但它与文件名方法一样强大。
使用路径中被忽略的部分作为版本号,服务器只是忽略它并提供未缓存的文件。
1.2.3/css/styles.css 提供与css/styles.css 相同的文件,因为第一个目录已被 htaccess 文件剥离并忽略
包括版本化文件
<?php
$version = "1.2.3";
?>
<html>
<head>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
<link rel="stylesheet" type="text/css" href="<?php echo $version ?>/css/styles.css">
</head>
<body>
<script src="<?php echo $version ?>/js/main.js"></script>
</body>
</html>
请注意,这种方法意味着您需要禁用索引页面的缓存 - Using <meta> tags to turn off caching in all browsers?
.htaccess 文件
RewriteEngine On
# if you're requesting a file that exists, do nothing
RewriteCond %{REQUEST_FILENAME} !-f
# likewise if a directory that exists, do nothing
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise, rewrite foo/bar/baz to bar/baz - ignore the first directory
RewriteRule ^[^/]+/(.+)$ $1 [L]
您可以在任何允许 url 重写的服务器平台上采用相同的方法
(改写条件改编自mod_rewrite - rewrite directory to query string except /#!/)
...如果您的索引页面/站点入口点需要缓存清除,您可以随时use JavaSript 刷新它。