Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inverted FilesMatch?

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/.

like image 569
Petr Abdulin Avatar asked Oct 17 '12 03:10

Petr Abdulin


3 Answers

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.

like image 95
Petr Abdulin Avatar answered Nov 10 '22 04:11

Petr Abdulin


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)
like image 30
Gabber Avatar answered Nov 10 '22 04:11

Gabber


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

like image 27
pogo Avatar answered Nov 10 '22 04:11

pogo