三种 .htaccess 过期规则之间的区别
以下三个 .htaccess 规则之间有什么区别以及何时使用每个规则,是否有任何特殊用例更喜欢其中一个?:
Header set Cache-Control "max-age=290304000"
Header set Expires "Thu, 15 Apr 2020 20:00:00 GMT"
ExpiresDefault "access plus 10 years"
What's the difference between the following three .htaccess rules and when to use each, is there any special use cases that prefers one over the other?:
Header set Cache-Control "max-age=290304000"
Header set Expires "Thu, 15 Apr 2020 20:00:00 GMT"
ExpiresDefault "access plus 10 years"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Header
是一个指令mod_headers 允许修改 HTTP 标头字段。在这种情况下,标头设置
有效地设置提到的标头字段Cache-Control 和 过期,因此已经存在的标头字段将被覆盖。第一个指令将标头字段 Cache-Control 设置为值
max-age=290304000
,该值将新鲜度生命周期描述为相对于响应时间的 290304000 秒。与此相反,第二个指令将标头字段 Expires 设置为值
Thu, 15 Apr 2020 20:00:00 GMT
,该值描述了新鲜度生命周期的绝对时间价值。两者Cache-Control的 max-age 值和 Expires 过期时间戳可以相互转换:
但是如果两者都存在,Cache-Control 的 max-age 优于 Expires:
无需手动设置这些 HTTP 缓存控制标头字段, mod_expires
ExpiresDefault
指令 允许轻松设置 HTTP 缓存。新鲜度生命周期可以用绝对值或相对值来描述,可以相对于响应时间(即access
/now
),也可以相对于修改时间请求的文件(即修改
)。它同时使用Cache-Control和Expires。在这种情况下,第三个指令将默认的新鲜度设置为从响应时间起 10 年。
我会使用 mod_expires 进行 HTTP 缓存控制,而不是使用
Header
手动执行。它更加方便,允许相对和绝对新鲜时间,并使用Cache-Control和Expires。Header
is a directive of mod_headers that allows to modify HTTP header fields. In this caseHeader set
effectively sets the mentioned header fields Cache-Control and Expires, so an already existing header field will be overwritten.The first directive sets the header field Cache-Control with the value
max-age=290304000
, that describes the freshness lifetime to be 290304000 seconds relative to the response time.In contrast to that, the second directive sets the header field Expires with the value
Thu, 15 Apr 2020 20:00:00 GMT
that describes the freshness lifetime with an absolute time value.Both Cache-Control’s max-age value and Expires expiration time stamp can be transformed to the other:
But if both are present, Cache-Control’s max-age is preferred over Expires:
Instead of setting these HTTP caching control header fields manually, mod_expires
ExpiresDefault
directive allows an easy setup for HTTP caching. The freshness lifetime can either be described with an absolute value or with a relative value, either relative to the response time (i.e.access
/now
) or relative to the modification time of the requested file (i.e.modification
). It uses both Cache-Control and Expires.In this case the third directive sets the default freshness lifetime to be 10 years from the time of response on.
I would use mod_expires for HTTP cache control instead of doing it manually with
Header
. It is far more convenient, allows both relative and absolute freshness times and uses both Cache-Control and Expires.第一条规则向 Cache-Control 添加 max-age 条目。浏览器必须在此处给出的时间(以秒为单位)之后重新获取文档。
第二条和第三条规则创建过期标头。浏览器必须在此处给出的日期重新获取文档。服务器必须做一些计算。
请注意,第二条规则强制同时刷新所有浏览器和所有资源,而第三条也是最后一条规则则根据请求时间使浏览器缓存失效。如果您的网站流量很大,您会看到差异:您可能会在 2020 年 4 月 15 日达到戏剧性的峰值。:)
根据经验:使用 max-age。 这是非常< a href="http://www.mnot.net/blog/2007/05/15/expires_max-age" rel="nofollow noreferrer">良好支持 并且您没有机会创建无效日期。此外,它更短。
进一步阅读:
Web 作者缓存教程
和网站管理员 – 必读
适用于每个 Web 开发人员。
Opera MAMA:HTTP 标头 – 一些有趣的内容统计信息,尤其是完整过期频率 表格很有趣:
The first rule adds a max-age entry to Cache-Control. The browser has to refetch the document after the time in seconds given here.
The second and the third rules create expires headers. The browser has to refetch the document at the date given here. And the server has to do some calculations.
Note that the second rule forces a refresh for all browsers and all resources at the same time, while the third and the last rule invalidate the browser cache depending on the request time. You will see the difference if you have a site with much traffic: You will probably get a dramatic peak at 15 Apr 2020. :)
As a rule of thumb: Use max-age. It is very good supported and you don’t have the chance to create an invalid date. Additionally, it’s shorter.
Further reading:
Caching Tutorial for Web Authors
and Webmasters – Required reading
for every web developer.
Opera MAMA: HTTP Headers – Some interesting statistics, especially the full Expires frequency table is funny: