阿帕奇:设置最大年龄或在.htaccess到期的目录 [英] 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-岁以上的过期或两者兼而有之?
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"
这将关闭ETag时,打开缓存控制:最大年龄
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>
我仍然在测试这个过程。我只是希望这不会关闭ETag时该网站的其余部分。到目前为止,它看起来像它的工作按计划进行。
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 - 未修改和最大年龄重置为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的日志文件,看到在/缓存目录项目开始担任一次。
All in all, you can watch the apache log file and see that items in /cache dir are begin served once.
请注意:我发现,设置标题附加的Cache-Control公
是确定的,如果你想要做的。
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>
检查标题应揭露这样的:
Inspecting the header should reveal this:
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
这将:
- 设置最大年龄为1年(最长推荐)
- 将期满1年标签
- 发送了ETag,所以1年后,浏览器将执行ETAG验证
- 让中间缓存设备/服务的知道,他们可以缓存文件为1年。
这篇关于阿帕奇:设置最大年龄或在.htaccess到期的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!