0

我正在尝试将过期标头添加到除某些特定文件之外的所有文件。事实上,我正在使用一个缓存工具,它将以下代码添加到我的 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)?$)">

也没有用。

我如何实现这一目标?

4

2 回答 2

0

您可以使用否定的lookbehind 正则表达式搜索。
看看这个regex101 游乐场

<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)?$)">
于 2019-03-20T21:17:44.120 回答
0

断言被禁止的文件名是不够的,因为以下捕获组可以匹配允许的文件扩展名。您应该能够使用经过调和的贪婪令牌来解决它:

^((?!\/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)?$)

演示

于 2018-07-08T21:08:46.310 回答