htaccess filesMatch exclusion
Solution 1
You've got a few capitalization errors in your config:
<filesMatch "\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml|html?)$">
^
should be <FilesMatch ...
<ifModule mod_headers.c>
^
should be <IfModule...
</ifModule>
^
should be </IfModule>
</filesMatch>
^
should be </FilesMatch>
Also, if you've got VirtualHosts, you need to make sure that you've got AllowOverride correctly configured
Solution 2
I think that you are looking for this:
<FilesMatch ".*$">
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</FilesMatch>
<FilesMatch "(?!\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml|html?))$">
FileETag None
<IfModule mod_headers.c>
Header set Cache-Control "max-age=3600
</IfModule>
</FilesMatch>
(?! ...)
is special syntax in Perl regular expressions and in PCRE, which is the regex library that Apache uses. It is negative lookahead assertion.
Solution 3
try to use
<Files ~ "\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml|html?)$">
instruction.
Also check that you have properly configured AllowOverride for this virtualhost
Related videos on Youtube
Hikari
Updated on September 18, 2022Comments
-
Hikari almost 2 years
I have the following directive in my htaccess
<filesMatch "\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml|html?)$"> FileETag None <ifModule mod_headers.c> Header unset ETag Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" Header set Pragma "no-cache" Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT" </ifModule> </filesMatch>
I copied that regex from someplace in Web months ago. It should add those headers to any HTTP Response that does NOT have those extensions.
But it's not working, it's adding them to any Response.
I also need to create another directive to add
Header set Cache-Control "max-age=3600, public"
to Responses of files that DOES have them.Could anybody help me make proper fileMatch regexes?
-
Hikari over 10 yearsYes I'd like to use FilesMatch, but thanks for the tip.
-
Hikari over 10 yearsThanks for the tip. This htaccess should be transparent for VHs, they are configured elsewhere. In my dev env I have none, in HMP there is 1, in production there are many. There's the need of the application config work anywhere it's deployed.
-
Hikari over 10 yearsThis is the FilesMatch for static files right? How should it be for excluding these extensions?
-
Jenny D over 10 yearsInstead of using a separate regexp to exclude the static extensions, start by setting the headers for all files outside of the FilesMatch directive. Then, the headers files matching the regexp for static files will be changed with the FilesMatch.
-
Hikari over 10 yearsI get it, but I'd like to leave default config and set mine to specific content types.
-
Jenny D over 10 yearsI don't quite understand your meaning. You've got one regexp that matches your static content. As I understood you, you also want one that matches everything else. There's no point in doing that since "everything else" will have the default config.
-
Hikari over 10 yearsah very sorry! lol now I got your logic
-
Hikari over 10 yearsThanks, could you explain what the
?!
means? -
Manolo over 10 yearsAnswer updated.