在 PHP 中重新加载/重定向页面的最佳方法是完全删除所有历史记录/缓存?我应该使用哪些标题?
怎么了:
单击链接时,会设置 get-parameters 并执行脚本。完成后,我想在没有获取参数的情况下重定向并重新加载页面。一开始看起来好像什么都没发生,但是当按下 F5 时,变化就出现了。
我想要的是:
重定向并重新加载,使更改无需按 F5 即可显示。
header('Location: http://www.example.com/', true, 302);
exit;
参考:http ://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
编辑:
此响应仅在由 Cache-Control 或 Expires 标头字段指示时才可缓存。
function redirect($url) {
if(!headers_sent()) {
//If headers not sent yet... then do php redirect
header('Location: '.$url);
exit;
} else {
//If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>';
exit;
}
}
// How to use
$url = "www.google.com";
redirect($url);
重新加载页面并强制其不从缓存中获取的最佳方法是将随机 id 或时间戳附加到 url 的末尾作为查询字符串。它使请求每次都是唯一的。
尝试这个:
echo '<script>document.location.replace("someurl.php");</script>';
这应该替换浏览器历史记录而不是缓存。
仅供参考,与 SEO 相关:
301 会告诉搜索引擎在他们的索引中替换 url。因此,如果 url1 使用 301 重定向到 url2,所有主要搜索引擎 [google, yahoo + bing] 都会将 url1 替换为 url2。
302 以不同的方式工作。它说该网址位于temporarily
其他地址。
header('Location: http://example.com/path/to/file');
<?php
header('Cache-Control: no-store, private, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0, max-stale = 0', false); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Expires: 0', false);
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header ('Pragma: no-cache');
header("Location: https://example.com", true, 302);
exit();
?>
最安全的方法是使用标头重定向
header('Location: http://www.example.com/', true, 302);
exit;
但请注意,它必须在任何其他输出发送到浏览器之前发送。