绕过缓存的 HTML 链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7062680/
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
HTML link that bypasses cache?
提问by lemon
I have a file that I link to from my website like
我有一个从我的网站链接到的文件,例如
<a href="http://example.com/myfile.txt>View!</a>
However, this file changes very frequently and when the link is clicked, the browser loads the cached version of the file, not the actual file. Is there a way so that clicking on that link will bypass the cache for that page?
但是,此文件更改非常频繁,单击链接时,浏览器会加载文件的缓存版本,而不是实际文件。有没有办法让点击该链接绕过该页面的缓存?
Something nice like <a bypassCache href="">
would be wishful thinking.
一些美好的事情<a bypassCache href="">
会是一厢情愿的想法。
回答by Pekka
Something nice like would be wishful thinking.
一些美好的事情会是一厢情愿的想法。
Indeed, there is something you can do from within the link: Add a random GET parameter.
实际上,您可以从链接中执行以下操作:添加随机 GET 参数。
<a href="http://example.com/myfile.txt?a=193834923283943842923">View!</a>
You could use JavaScript (or of course a server-side scripting language like PHP) to do this on a dynamic basis.
您可以使用 JavaScript(当然也可以使用像 PHP 这样的服务器端脚本语言)以动态方式执行此操作。
However, the far superior way would be to configure the text file's caching headers correctly in the first place on server side. Stealing the header info from Best way to disable client caching, a .htaccess file like this should work:
但是,更好的方法是首先在服务器端正确配置文本文件的缓存标头。从Best way to disable client caching窃取标头信息,像这样的 .htaccess 文件应该可以工作:
<Files myfile.txt>
FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control "store, no-cache, must-revalidate, post-check=0, pre-check=0"
Header set Pragma "no-cache"
Header set Expires "Sun, 19 Nov 1978 05:00:00 GMT"
</IfModule>
</FilesMatch>
回答by Job
Just put
就放
<meta http-equiv="expires" content="0">
Into the head section of your target page and check again
进入目标页面的头部部分并再次检查
回答by Puggan Se
The best way is to tell apache/(web server) to tell browser not allow caching of that file, if you don't have controll over that server, you could avoid cache by alter the parameters send to it, just add some numbers behind ?, for exemple the time when you created the link, this makes each url diferent, so the browser going to ignore the cache, but all links to the same file, as long as the server ignore the extra parameter. in php:
最好的方法是告诉 apache/(web server) 告诉浏览器不允许缓存该文件,如果您无法控制该服务器,则可以通过更改发送给它的参数来避免缓存,只需在后面添加一些数字?,例如你创建链接的时间,这使得每个url不同,所以浏览器会忽略缓存,但是所有链接到同一个文件,只要服务器忽略额外的参数。在 php 中:
echo "<a href='http://example.com/myfile.txt?" . time() . "'>View!</a>"
回答by mkk
You can solve your problem on server level. Set the special expiration date for txt resources (or that particular one) that fit to your requirements. If you use apache you can read about mod_expiryfor apache here
您可以在服务器级别解决您的问题。为符合您要求的 txt 资源(或特定资源)设置特殊的到期日期。如果您使用 apache,您可以在此处阅读有关apache 的mod_expiry的信息
回答by Raptor
Add a random number after the hyper link, such as <a href="http://example.com/myfile.txt?rand=12312321321">View!</a>
在超链接后添加一个随机数,如 <a href="http://example.com/myfile.txt?rand=12312321321">View!</a>
Generate a new random number each time the page loads.
每次页面加载时生成一个新的随机数。