0

I'm trying to add expires headers to all files except some specific files. In fact, I'm using a caching tool, that adds the following code to my 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

I want to make an exception for two different images on my page. Let their names be file-a and file-b, here is what I tried:

I put this code after the code from the plugin.

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

Since that didn't work, I also tried putting it before the code, which didn't work either.

Then I tried manipulating the FilesMatch part of the code added by the plugin, so that it excludes my files.

<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)?$)">

Didn't work either.

How do I achieve this?

TheKidsWantDjent
  • 1,179
  • 1
  • 9
  • 20

2 Answers2

0

It is not enough to assert the banned files names since the following capture group can sill match the allowed file extensions. You should be able to solve it using a 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

wp78de
  • 18,207
  • 7
  • 43
  • 71
0

You can use a negative lookbehind regex search.
Check out this 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)?$)">
Jun
  • 2,942
  • 5
  • 28
  • 50