问题描述
托管我的网站的网络服务器未返回上次修改或到期的标头.我想纠正此问题,以确保我的Web内容可缓存.
The webserver hosting my website is not returning last-modified or expiry headers. I would like to rectify this to ensure my web content is cacheable.
我无权访问apache配置文件,因为该站点托管在我无法控制的共享环境中.但是,我可以通过.htaccess文件进行配置.服务器-Apache 1.3-未配置mod_expires或mod_headers,该公司将不会为我安装这些服务器.
I don't have access to the apache config files because the site is hosted on a shared environment that I have no control over. I can however make configurations via an .htaccess file. The server - apache 1.3 - is not configured with mod_expires or mod_headers and the company will not install these for me.
考虑到这些限制,我有什么选择?
With these limitations in mind, what are my options?
很抱歉,此处的帖子.我知道这个问题严格来说不是编程问题,更不是sys admin问题.当serverfault公开时,我将确保在此处直接提出这种性质的问题.
Sorry for the post here. I recognise this question is not strictly a programming question, and more a sys admin question. When serverfault is public I'll make sure I direct questions of this nature there.
推荐答案
什么样的内容?如果是静态的(HTML,图像,CSS),那么真正附加标题的唯一方法是通过前端Web服务器.我很惊讶托管公司没有启用mod_headers,尽管他们可能没有为.htaccess启用它.不缓存就使他们付出了更多的带宽和CPU(即金钱)的代价.
What sort of content? If static (HTML, images, CSS), then really the only way to attach headers is via the front-end webserver. I'm surprised the hosting company doesn't have mod_headers enabled, although they might not enable it for .htaccess. It's costing them more bandwidth and CPU (ie, money) to not cache.
如果是动态内容,则在生成页面时将具有控制权.这将取决于您的语言.这是PHP的示例(来自PHP手册,是一个糟糕的示例,因为它还应设置响应代码):
If it's dynamic content, then you'll have control when generating the page. This will depend on your language; here's an example for PHP (it's from the PHP manual, and is a bad example, as it should also set the response code):
if (!headers_sent()) {
header('Location: http://www.example.com/');
exit;
}
哦,还有关于设置缓存头的一件事:不要设置太长的时间,尤其是对于CSS和脚本.您可能不希望更改这些内容,但是当人们的浏览器中仍然有旧内容时,您不希望网站损坏.我建议将最大缓存设置设置在4-8小时范围内:这对于单个用户的会话或工作日来说非常有用,但不多.
Oh, and one thing about setting caching headers: don't set them for too long a duration, particularly for CSS and scripts. You may not think you want to change these, but you don't want a broken site while people still have the old content in their browsers. I would recommend maximum cache settings in the 4-8 hour range: good for a single user's session, or a work day, but not much more.
这篇关于如何配置没有mod_expires或mod_headers的Apache发送到期标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!