在Apache服务器上使用.htaccess实现缓存控制

发布于 2024-10-10 17:11:55 字数 709 浏览 0 评论 0原文

好吧,我仍在尝试了解一些缓存内容,并且我已经浏览了一些可以在 Google 上找到的示例。我已将以下代码添加到我的 .htaccess 文件中:

### activate mod_expires
ExpiresActive On
### Expire .gif's 1 month from when they're accessed
ExpiresByType image/gif "access plus 3 months"
ExpiresByType image/png "access plus 3 months"
ExpiresByType image/jpg "access plus 3 months"
ExpiresByType text/javascript "access plus 3 months"

使用 Chrome 审核工具和 YSlow Firebug 工具,看起来这正在缓存我的一些图像/文件,但到目前为止还不是全部。我仍然有一个未缓存的文件列表(.jpg、.js 和 .css - 我知道我没有将 css 文件设置为缓存)。 Chrome 审核中的消息只是指出以下资源缺少缓存过期。未指定过期时间的资源可能不会被浏览器缓存:

一些未缓存的图像是背景图像,其他图像是 js 库的一部分,并且它们是通过 JS 调用的 - 可以吗会影响为什么他们不缓存?

抱歉,我无法提供代码链接 - 该网站仍处于保密状态,仅限客户查看。

提前致谢!

okay, I'm still trying to get my head around some of the caching stuff and I have gone through a couple of examples I could find on Google. I have added the following code to my .htaccess file:

### activate mod_expires
ExpiresActive On
### Expire .gif's 1 month from when they're accessed
ExpiresByType image/gif "access plus 3 months"
ExpiresByType image/png "access plus 3 months"
ExpiresByType image/jpg "access plus 3 months"
ExpiresByType text/javascript "access plus 3 months"

Using the Chrome audit tools and the YSlow Firebug tool, it looks like this is caching some of my images/files, but not by far all of them. I still have a list of files (.jpg, .js and .css - I know I've not set the css files to cache here) that aren't caching. The message in the Chrome audit simply states The following resources are missing a cache expiration. Resources that do not specify an expiration may not be cached by browsers:

some of the images that aren't caching are background images, others are part of a js gallery and they're being called via the JS - could that be affecting why they aren't caching?

Sorry I can't give a link to the code - the sites still under wraps and limited to client view only.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2024-10-17 17:11:55

看来您写错了 MIME 类型:

# enable expirations
ExpiresActive On
ExpiresDefault "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/pjpeg "access plus 1 week"
ExpiresByType text/javascript "modification plus 1 week"
ExpiresByType application/javascript "modification plus 1 week"
ExpiresByType text/css "modification plus 1 week"

It looks like you've written the MIME-types wrong:

# enable expirations
ExpiresActive On
ExpiresDefault "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/pjpeg "access plus 1 week"
ExpiresByType text/javascript "modification plus 1 week"
ExpiresByType application/javascript "modification plus 1 week"
ExpiresByType text/css "modification plus 1 week"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文