htaccess/mod_expires-缓存特定文件 [英] htaccess / mod_expires - caching specific files

查看:165
本文介绍了htaccess/mod_expires-缓存特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我检查了很多网站,了解如何使用.htaccess文件管理浏览器的缓存,但是对我来说仍然不清楚.

Ok, I checked a lot of websites about how to manage the browser's cache memory with a .htaccess file, but it is still very not clear to me.

我想将特定文件缓存一个月.对于其余的内容,我希望每次都可以刷新.所以我尝试了:

I want to cache specific files for one month. For the rest, I want it to be refreshed every time. So I tried:

<IfModule mod_headers.c> 
    Header unset Cookie
    Header unset Set-Cookie
    Header unset Cache-Control
    Header unset ETag
    FileETag none 
</IfModule>

<IfModule mod_expires.c>  
    ExpiresActive On
    ExpiresDefault "now"
    <Files "/css/jquery-ui.css">
      ExpiresDefault "access plus 1 month"
    </Files>
    <Files "/js/jquery-1.10.2.min.js">
      ExpiresDefault "access plus 1 month"
    </Files>
    <Files "/js/jquery-ui.js">
      ExpiresDefault "access plus 1 month"
    </Files>
    <Files "/js/analytics.js">
      ExpiresDefault "access plus 1 month"
    </Files>
    <Files "/matheos/img/*">
      ExpiresDefault "access plus 1 month"
    </Files>
    <Files "/img/*">
      ExpiresDefault "access plus 1 month"
    </Files>
</IfModule>

但是它不能完全按预期工作...

But it doesn't work exactly as expected...

正确地没有缓存HTML,但是也没有缓存诸如jquery-ui.css之类的特定文件,这些文件应被缓存1个月.

The HTML is correctly not cached, but the specific files like jquery-ui.css, which should be cached for 1 month, are also not cached.

无论如何,这个.htaccess对您来说还好吗?

Anyway, does this .htaccess seem ok for you ?

推荐答案

好,知道了!要定位特定文件,正确的语法是:

Ok, got it ! To target a specific file, the correct syntax is :

# to not cache css except jquery-ui.css
ExpiresByType text/css "now"
<FilesMatch "jquery-ui\\.css$">
    ExpiresByType text/css "access plus 1 month"
</FilesMatch>

这是对我有用的唯一方法,至少在ovh共享主机的情况下.我还尝试了ExpiresDefault的所有可能组合,但是没有用...

This is the only way that worked for me, at least in the case of an ovh shared host. I also tried all possible combinations with ExpiresDefault but it didn't work...

这篇关于htaccess/mod_expires-缓存特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆