I'm setting cache control headers for files. I want to set max-age=86400
for all .css
and .js
, and max-age=3600
for all others.
<FilesMatch "\.(css|js)$">
Header append Cache-Control max-age=86400
</FilesMatch>
<FilesMatch "???">
Header append Cache-Control max-age=3600
</FilesMatch>
I can't figure out what regex I should write to invert \.(css|js)$
match. Or maybe there is some other way to do this?
UPDATE. Based on this question answer I've found solution that works:
<FilesMatch "(?<!\.css|\.js)$">
Header append Cache-Control max-age=3600
</FilesMatch>
Unfortunatelly can't find a way to leave dot \.
outside of brackets. But still this solution fine for me.
As a side note, all other files include ones with not filename at all, like http://example.com/
.
Based on this question answer I've found solution that works:
<FilesMatch "(?<!\.css|\.js)$">
Header append Cache-Control max-age=3600
</FilesMatch>
Unfortunatelly can't find a way to leave dot \.
outside of brackets. But still this solution fine for me.
You should go check to the end of the string for the real file extension (which is the last, non dot-containing substring)
.+\.(?!(css|js)$)[^\.]+?$
Explanation:
.+\.
Search greedily the whole string to find the last dot.(?!(css|js)$)
skip any .css
or .js
terminating to the end of the line (but keep for example .jsx
)[^\.]+?$
get the extension part (no dots until the end of the string)You can try this.
.+?\.(?!(css$|js$)).+
The first group .+?
gets the file name and the second negative look ahead group ?!(css$|js$)
checks to see that the file ending is not css or js. This matches only files which don't have .css or .js extensions. You can replace the first and the last . with character class based on the filename characters permitted.
Edited: This will match test.cssx or test.jxabc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With