htaccess的缓存与FilesMatch缓存、htaccess、FilesMatch

2023-09-02 00:36:29 作者:tū嘫鋒悧の徊yì…

我需要让浏览器缓存与htaccess文件。

I need to make browser caching with htaccess file.

从this问题我发现了如何扩展添加到htaccess文件。

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

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

但我需要添加扩展。但排除某些文件。 我发现像这样的this问题

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

添加所有的JS和JPEG文件,除了myfile.js,myfile2.js,myphoto.jpg 我怎样才能做到这一点? 谢谢

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

推荐答案

试试这个

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

这将匹配所有的 JS JPEG 除了 myfile.js , myfile2.js myphoto.jpeg 使用负向前查找/后向。有点难看,但我无法找到一个很好的方法来做到这一点。

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>
 
精彩推荐
图片推荐