.htaccess cache-control不会缓存我的文件(js | png | css等)

时间:2012-09-27 08:31:32

标签: apache .htaccess caching cache-control

正如我的标题所说,我正在尝试缓存控制我网站上的内容,但似乎我的内容没有被缓存。

这是我的.htaccess文件中的代码:

Header unset Pragma
FileETag None
Header unset ETag

# cache images/pdf docs for 10 days
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">
Header set Cache-Control "max-age=864000, public, must-revalidate"
Header unset Last-Modified
</FilesMatch>

# cache html/htm/xml/txt diles for 2 hours
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>

我已经尝试过很多教程,但似乎都没有。 顺便说一句,其他.htaccess代码,如重定向和东西,都可以工作。

1 个答案:

答案 0 :(得分:14)

我想通了,你必须删除Header unset Last-Modified

Header unset Pragma
FileETag None
Header unset ETag

# cache images/pdf docs for 10 days
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">
Header set Cache-Control "max-age=864000, public, must-revalidate"
</FilesMatch>

# cache html/htm/xml/txt files for 2 hours
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>