在 303 重定向时,您必须指定重定向 URI:
<?php
header('HTTP/1.1 303 See Other');
header('location: http://www.example.com/some-url/');
作为缓存 301 响应的解决方法,您可以将过期日期设置为过去。这种方式鼓励客户立即将响应标记为过期:
<?php
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Expires: Sat, 1 Feb 1997 06:00:00 GMT');
303 响应不应被任何尊重RFC 2616 的客户端缓存:
303 响应不能被缓存,但对第二个响应的响应
(重定向的)请求可能是可缓存的。
考虑到上述情况,您可以将初始代码 sn-p 更改为如下内容:
if ('POST' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) {
// process post request
// set http status code
header('HTTP/1.1 303 See Other');
header('Location: http://www.example.com/newurl.php'); // FULL URI HERE
exit;
}
// set http status code
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.example.com/other-page.php');
但是,在您的情况下,307 重定向应该更合适。尽管 307 是一个临时重定向,并且您将始终在任何不是 POST 的请求上进行重定向,但您可以在将来更改此行为,因为根据 RFC "由于重定向有时可能会更改,客户端应该继续使用未来请求的请求 URI"。第二个优点是:“此响应仅在由 Cache-Control 或 Expires 标头字段指示时才可缓存。”
请求的资源暂时位于不同的 URI 下。
由于重定向有时可能会改变,客户端应该
继续使用 Request-URI 来处理未来的请求。此回复
只有在 Cache-Control 或 Expires 标头指示时才可缓存
字段。
见RFC 2616 秒。 10.3.8