Apache:在 .htaccess 中为目录设置 max-age 或 expires [英] Apache: set max-age or expires in .htaccess for directory
问题描述
我有一些目录的内容永远不会改变.
I have a handful of directories with content which will never change.
是否可以创建 .htaccess 文件来告诉浏览器该目录和子目录中的任何内容都可以缓存很长时间?
Is it possible to create .htaccess file which tells the browser that anything in this directory and sub- directories can be cached for a very long time?
如果可能,我想在每个目录中复制相同的 .htaccess 文件.
I would like to copy the same .htaccess file in each directory if possible.
如果可以的话,您会推荐 max-age over expires 还是两者兼而有之?
If this is possible would you recommend max-age over expires or both?
推荐答案
所以看起来确实可行.....htaccess 文件的语法是:
So it does look possible.... the .htaccess file syntax is:
Header unset Last-Modified
FileETag none
ExpiresActive On
ExpiresDefault "access plus 1 years"
这将关闭 Etags 并打开缓存控制:max-age
This will turn off Etags and turn on cache-control: max-age
然后把这个.htaccess文件放在目录下,所有文件(包括它的子目录)都会缓存1年.
Then put this .htaccess file in the directory and all files (including it's sub-directories will be cached for 1 year.
我决定将所有可缓存的内容放在一个根目录下,并将 httpd.conf
编辑为
I decided to put all my cache-able content under a single root directory and edit the httpd.conf
as
<Directory "C:\somedir\cache">
Header unset Last-Modified
FileETag none
ExpiresActive On
ExpiresDefault "access plus 1 years"
</Directory>
我仍在对此进行测试.我只是希望这不会关闭网站其余部分的 Etags.到目前为止,它看起来正在按计划运行.
I am still in the process of testing this. I just hope this does not turn off Etags for the rest of the site. So far it looks like it's working as planned.
更新(6 个月后):
设置 ExpiresDefault 并允许使用电子标签是最好的做法.
Setting the ExpiresDefault and allowing e-tags is the best thing to do.
在 httpd.conf 中:
in httpd.conf:
<Directory "C:\somedir\cache">
ExpiresActive On
ExpiresDefault "access plus 1 year"
</Directory>
确保somedir"在 apache 根目录内(例如 htdocs).
Make sure "somedir" is inside of the apache root (such as htdocs).
允许电子标签很好,因为 1 年后,浏览器将通过传递电子标签重新验证文件.Web 服务器将发回 304 - Not Modified 并将 max-age 重置为 1 年.这非常有效.
Allowing e-tags is a good because after 1 year, the browser will re-validate the file by passing the e-tag. The web server will send back a 304 - Not Modified and reset the max-age to 1 year. This is very efficient.
总而言之,您可以查看 apache 日志文件并看到/cache 目录中的项目开始提供一次.
All in all, you can watch the apache log file and see that items in /cache dir are begin served once.
注意:我发现如果需要,可以设置 Header append Cache-Control "public"
.
Note: I have found that setting Header append Cache-Control "public"
is ok to do if you want.
最终版本:
这是最终版本:(只需在 httd.conf 底部添加)
Here's the final version: (just add this at the bottom of the httd.conf)
<Directory "C:\somedir\cache">
ExpiresActive On
ExpiresDefault "access plus 1 year"
Header append Cache-Control "public"
</Directory>
检查标题应该会发现:
Accept-Ranges:bytes
Cache-Control:max-age=31536000, public
Connection:Keep-Alive
Content-Language:en
Content-Length:746
Content-Type:text/css
Date:Thu, 29 May 2014 15:23:50 GMT
ETag:"240000000add63-2ea-4f4086d72ad01"
Expires:Fri, 29 May 2015 15:23:50 GMT
Keep-Alive:timeout=40, max=200
Last-Modified:Fri, 07 Mar 2014 18:28:59 GMT
这将:
- 将 max-age 设置为 1 年(推荐的最长)
- 发送 1 年的过期标签
- 发送一个 Etag,这样 1 年后浏览器就会执行 etag 验证
- 让中间缓存设备/服务知道他们可以将文件缓存 1 年.
这篇关于Apache:在 .htaccess 中为目录设置 max-age 或 expires的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!