2

如何使用 htaccess 将图像在我的网站上缓存 2 天,以便:

1 x 60 x 60 x 24 x 2 = 172800s

所以我想缓存'png,jpeg,jpg,ico,js'。我怎样才能用 htaccess 做到这一点

4

1 回答 1

2

您可以使用该mod_expires模块(基于文件的 MIME 类型):

<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault A300

# png, jpeg, jpg, ico, js expire after 2 days
ExpiresByType image/gif A172800
ExpiresByType image/png A172800
ExpiresByType image/jpg A172800
ExpiresByType image/x-icon A172800
ExpiresByType application/x-javascript A172800
</ifModule>

mod_headers模块(基于文件扩展名):

<ifModule mod_headers.c>
ExpiresActive On

# png, jpeg, jpg, ico, js expire after 2 days
<filesMatch ".(gif|png|jpg|jpeg|ico|js)$">
Header set Cache-Control "max-age=172800"
</filesMatch>
</ifModule>

最后一个为您提供了更多选项,例如强制无缓存等。

于 2014-04-27T09:27:49.363 回答