我们的调查表明,并非所有浏览器都以统一的方式尊重HTTP缓存指令。
出于安全原因,我们不希望应用程序中的某些页面被web浏览器缓存。这必须至少适用于以下浏览器:
Internet Explorer 6+Firefox 1.5+Safari 3+歌剧9+铬
我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮并查看缓存的页面。
我们的调查表明,并非所有浏览器都以统一的方式尊重HTTP缓存指令。
出于安全原因,我们不希望应用程序中的某些页面被web浏览器缓存。这必须至少适用于以下浏览器:
Internet Explorer 6+Firefox 1.5+Safari 3+歌剧9+铬
我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮并查看缓存的页面。
当前回答
不确定我的答案听起来是否简单和愚蠢,也许你早就知道了,但由于阻止某人使用浏览器后退按钮查看历史页面是你的目标之一,你可以使用:
window.location.replace(“https://www.example.com/page-not-to-be-viewed-in-browser-history-back-button.html");
当然,这可能不可能在整个网站上实现,但至少对于一些关键页面,您可以做到这一点。希望这有帮助。
其他回答
头函数的PHP文档有一个相当完整的示例(由第三方提供):
header('Pragma: public');
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0', false); // HTTP/1.1
header ("Pragma: no-cache");
header("Expires: 0", false);
完成BalusC->答案如果您使用的是perl,则可以使用CGI添加HTTP标头。
使用Perl:
Use CGI;
sub set_new_query() {
binmode STDOUT, ":utf8";
die if defined $query;
$query = CGI->new();
print $query->header(
-expires => 'Sat, 26 Jul 1997 05:00:00 GMT',
-Pragma => 'no-cache',
-Cache_Control => join(', ', qw(
private
no-cache
no-store
must-revalidate
max-age=0
pre-check=0
post-check=0
))
);
}
使用apachehttpd.conf
<FilesMatch "\.(html|htm|js|css|pl)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
注意:当我尝试使用html META时,浏览器忽略了它们并缓存了页面。
RFC for HTTP 1.1指出,正确的方法是为以下内容添加HTTP标头:
缓存控制:无缓存
如果较旧的浏览器不符合HTTP 1.1,它们可能会忽略这一点。对于那些你可以尝试标题:
Pragma:无缓存
这也适用于HTTP1.1浏览器。
对于ASP.NET Core,创建一个简单的中间件类:
public class NoCacheMiddleware
{
private readonly RequestDelegate m_next;
public NoCacheMiddleware( RequestDelegate next )
{
m_next = next;
}
public async Task Invoke( HttpContext httpContext )
{
httpContext.Response.OnStarting( ( state ) =>
{
// ref: http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
httpContext.Response.Headers.Append( "Cache-Control", "no-cache, no-store, must-revalidate" );
httpContext.Response.Headers.Append( "Pragma", "no-cache" );
httpContext.Response.Headers.Append( "Expires", "0" );
return Task.FromResult( 0 );
}, null );
await m_next.Invoke( httpContext );
}
}
然后在Startup.cs中注册
app.UseMiddleware<NoCacheMiddleware>();
确保在以下位置添加此
app.UseStaticFiles();
您可以在IIS中使用位置块来设置单个文件,而不是整个应用程序获取缓存
<location path="index.html">
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>