apache 如何强制浏览器刷新网页的缓存版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1207492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force a browser to refresh a cached version of a webpage
提问by Chris
I have a website that because of an ill-prepared apache conf file has instructed users to cache a website URL several years into the future. As a result, when a person visits the site, they often make no attempt to even request the page. The browser just loads the HTML from cache.
我有一个网站,由于准备不充分的 apache conf 文件指示用户在未来几年缓存网站 URL。因此,当一个人访问该站点时,他们通常甚至不会尝试请求该页面。浏览器只是从缓存中加载 HTML。
This website is about to get a major update, and I would like for users to be able to see it. Is there a way for me to force a user to actually re-request the webpage? I fear that for some users, unless they happen to press F5, they may see the old webpage for several years.
本网站即将进行重大更新,我希望用户能够看到它。有没有办法强迫用户实际重新请求网页?我担心对于某些用户来说,除非他们碰巧按 F5,否则他们可能会看到几年的旧网页。
回答by liori
Use different URLs. If the main entry point to your website (like the main index file) is cached, then you're screwed... maybe you should register another domain name?
使用不同的 URL。如果您网站的主要入口点(如主索引文件)被缓存,那么您就完蛋了……也许您应该注册另一个域名?
回答by Sorantis
There are several options to achieve this First In the section add meta tag:
有几个选项可以实现这个首先在添加元标记的部分:
<meta http-equiv="pragma" content="no-cache" />
Basically the browser won't cache the page.
基本上浏览器不会缓存页面。
Another option is to set sessions, this will force the browser to a new session each time they go, and thus make the browser get the page from the server instead of the cache
另一种选择是设置会话,这将强制浏览器每次访问时进入一个新会话,从而使浏览器从服务器而不是缓存中获取页面
<?php
session_start();
$_SESSION = array();
session_destroy();
?>
You can add this to your website for a couple of days and then remove. I really don't know if it will do, but perhaps you will find it useful
您可以将其添加到您的网站几天,然后删除。我真的不知道它是否会这样做,但也许你会发现它很有用
回答by biophonc
It is arguable that if your "major update" is just in a few (2 or 3) weeks, you only need to reconfigure your apache conf now (no far future stuff for html - only for assets and content that will most likely never change). The Firefox cache is ~50MB by default and that is not much because images get also cached and modern websites have a lot of content.
有争议的是,如果您的“重大更新”只是在几个(2 或 3)周后,您现在只需要重新配置您的 apache conf(对于 html 来说没有远期的东西 - 仅适用于最有可能永远不会改变的资产和内容)。默认情况下,Firefox 缓存大约为 50MB,这并不多,因为图像也会被缓存并且现代网站有很多内容。
Not perfect - but thats what I would do - when I don't want to or can't change the URL's ;)
不完美 - 但这就是我会做的 - 当我不想或无法更改 URL 时;)
回答by alexanderbird
Years late, but it might help someone down the road: if you've got anyjavascript file that isn't cached years into the future (i.e. if you have any way of running new js on the cached site), add some js that will programatically clear the cache. Once the configuration is fixed and/or the update complete, remove the cache clearing js.
晚了几年,但它可能会对未来的人有所帮助:如果您有任何未缓存多年的 javascript 文件(即,如果您有任何方法可以在缓存的站点上运行新的 js),请添加一些 js将以编程方式清除缓存。修复配置和/或更新完成后,删除缓存清除 js。
回答by Carmelo Vargas
I realize this question is very old, but I have a viable answer:
我意识到这个问题很老了,但我有一个可行的答案:
Of course you can force new URL's to avoid common caches (not long term ones)...
当然,您可以强制使用新 URL 以避免常见缓存(不是长期缓存)...
I.e.
IE
- Add query to static .js/.css/.html files
- Add meta pragma tag for no-cache
- Server-side redirections, no cache, eliminate cookies, sessions, etc.
- 向静态 .js/.css/.html 文件添加查询
- 为无缓存添加元编译指示标签
- 服务器端重定向,无缓存,消除 cookie、会话等。
However in a scenario like this (formerly edited apache .conf for long time caching) since you cannot change the domain for SEO purposes, there is a 'crude hack' you can use which will minimize the impact to SEO:
然而,在这样的情况下(以前编辑 apache .conf 以进行长时间缓存),由于您无法出于 SEO 目的更改域,因此您可以使用“粗略的黑客”来最大限度地减少对 SEO 的影响:
Copy your index page (i.e. index.php) to a new page (i.e. index_new.php) and edit httpd.conf (or equivalent) so that the DirectoryIndex is the new page. Then just delete or move your old page, it should theoretically always redirect to the new page.
将您的索引页面(即 index.php)复制到一个新页面(即 index_new.php)并编辑 httpd.conf(或等效文件),以便 DirectoryIndex 成为新页面。然后只需删除或移动旧页面,理论上它应该始终重定向到新页面。
I.e. in Debian/Ubuntu:
即在 Debian/Ubuntu 中:
cd /var/www
cp index.php index_new.php
sudo vim /etc/apache2/sites-enabled/000-default
<Directory /var/www/>
...
DirectoryIndex index_new.php
</Directory>
mv index.php index_old.php
sudo service apache2 restart
And there you go.
你去吧。
回答by Hyman Ryan
I think that there is no way of doing this. If they never contact your sever there really is nothing you can do about it.
我认为没有办法做到这一点。如果他们从不联系您的服务器,那么您真的无能为力。
回答by Alex. S.
change URLs of every resource
更改每个资源的 URL
回答by Camilo
You can make all the necessary changes inside the HTML files, like
您可以在 HTML 文件中进行所有必要的更改,例如
<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0" />
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
But also, you can explicitely tell Apache to use mod_expires.c module, and add a couple of directives to httpd.conf file:
但是,您也可以明确地告诉 Apache 使用 mod_expires.c 模块,并向 httpd.conf 文件添加几个指令:
<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresByType text/html "modification plus 5 seconds"
ExpiresByType text/javascript "modification plus 5 seconds"
ExpiresDefault "access plus 2 days"
</IfModule>
This way you add the http headers cache-control and expires, to responses, in order for thw browser to update the cache 5 seconds after the file was modified in the origin, for those kinds of files, and 2 days after being accessed by the browser for all the other kinds of files.
通过这种方式,您将 http 标头缓存控制和过期添加到响应中,以便浏览器在源中修改文件后 5 秒更新缓存,对于这些类型的文件,以及在被访问后 2 天所有其他类型文件的浏览器。
Hope this helps.
希望这可以帮助。

