.htaccess caching with FilesMatch

2019-06-24 11:05发布

I need to make browser caching with htaccess file.

From this question I found out how to add extensions to htaccess file.

<FilesMatch "\.(js|jpeg|jpg)$">

But I need to add extensions. But exclude some of the files.
I found something like this from this question

<FilesMatch ^((myfile|myfile2)\.js$|myphoto\.jpe?g)$>

Add all js and jpeg files except "myfile.js", "myfile2.js", "myphoto.jpg" How can I do this? Thank you

1条回答
萌系小妹纸
2楼-- · 2019-06-24 11:34

Try this

<FilesMatch "((?<!myfile|myfile2)\.js|(?<!myphoto).jpe?g)$">
    Header set Cache-Control "max-age=604800, public"
</FilesMatch>

This will match all js and jpeg files except myfile.js, myfile2.js, and myphoto.jpeg using negative lookahead/lookbehind. Kind of ugly but I couldn't find a nice way to do this.

You can then have a separate files match for only those files and set a different header:

<FilesMatch "((myfile|myfile2)\.js|myphoto\.jpe?g)$">
    Header set Cache-Control "max-age=3600, public" 
</FilesMatch>
查看更多
登录 后发表回答