【问题标题】:Add expires header for all files except specific files为除特定文件之外的所有文件添加过期标头
【发布时间】:2019-03-21 05:17:44
【问题描述】:

我正在尝试将过期标头添加到除某些特定文件之外的所有文件。事实上,我正在使用一个缓存工具,它将以下代码添加到我的 htaccess 中:

# BEGIN LBCWpFastestCache
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|webp|js|css|swf|x-html|css|xml|js|woff|woff2|ttf|svg|eot)(\.gz)?$">
<IfModule mod_expires.c>
AddType application/font-woff2 .woff2
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/webp A2592000
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType image/svg+xml A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000
ExpiresByType application/javascript A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType application/font-woff2 A2592000
</IfModule>
<IfModule mod_headers.c>
Header set Expires "max-age=2592000, public"
Header unset ETag
Header set Connection keep-alive
FileETag None
</IfModule>
</FilesMatch>
# END LBCWpFastestCache

我想为我页面上的两个不同图像设置一个例外。让他们的名字是 file-a 和 file-b,这是我尝试过的:

我把这段代码放在插件代码之后。

<FilesMatch "\.(file-a|file-b)$">
ExpiresDefault "access plus 1 hour"
</FilesMatch>

既然这样不行,我也试过把它放在代码前面,也不管用。

然后我尝试操作插件添加的代码的FilesMatch 部分,以便它排除我的文件。

<FilesMatch "(?!.*/(file-a|file-b))(\.(ico|pdf|flv|jpg|jpeg|png|gif|webp|js|css|swf|x-html|css|xml|js|woff|woff2|ttf|svg|eot)(\.gz)?$)">

也没有用。

我如何做到这一点?

【问题讨论】:

    标签: .htaccess caching expires-header


    【解决方案1】:

    断言被禁止的文件名是不够的,因为以下捕获组可以匹配允许的文件扩展名。您应该可以使用tempered greedy token 解决它:

    ^((?!\/file-a|file-b).)*(\.(ico|pdf|flv|jpg|jpeg|png|gif|webp|js|css|swf|x-html|css|xml|js|woff|woff2|ttf|svg|eot)(\.gz)?$)
    

    Demo

    【讨论】:

      【解决方案2】:

      您可以使用否定的后向正则表达式搜索。
      看看这个regex101 playground

      <FilesMatch "(?<!file-a|file-b)(\.(ico|pdf|flv|jpg|jpeg|png|gif|webp|js|css|swf|x-html|css|xml|js|woff|woff2|ttf|svg|eot)(\.gz)?$)">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-12
        • 1970-01-01
        • 1970-01-01
        • 2014-07-25
        • 1970-01-01
        • 2012-12-15
        • 1970-01-01
        • 2012-07-21
        相关资源
        最近更新 更多