4

我没有生成指向 的链接file.js,而是计算版本号或哈希总和并链接到file--bbe02f946d.js. 我正在使用以下重定向规则来提供文件的当前版本:

RewriteRule ^(.*)--[a-z0-9]+\.js$ $1.js

现在,我想为这些请求设置非常遥远的Expires标头:

ExpiresActive on
ExpiresByType application/javascript "access plus 1 year"

这工作正常,但也适用于尚未版本化的资源(/file.js请求)。如何仅为与 RewriteRule 匹配的请求设置过期标头?通常,我会使用<LocationMatch>,但这不可用,因为应用程序必须能够在我可以修改 htaccess 的任意服务器上运行。

4

1 回答 1

0

您可以使用 htaccess 添加新的 mime 类型并设置自定义过期标头。

使用 .xjs 的示例

<IfModule mod_mime.c>
    AddType application/x-javascript .xjs
</IfModule>

<IfModule mod_gzip.c>
mod_gzip_item_include file \.xjs$
</IfModule>

<FilesMatch ".(xjs)$">
Header set Cache-Control "max-age=43200"
</FilesMatch>

或者只是使用正则表达式FilesMatch来匹配你的 js 文件

<FilesMatch "--[a-z0-9]+\.js$">
Header set Cache-Control "max-age=43200"
</FilesMatch>
于 2014-04-08T14:37:15.973 回答