在 Apache 级别设置规则,发现 php 中的 header('Location: filename.php') 覆盖了响应代码。看看下面的代码:-
<?php
header('HTTP/1.1 308 Permanent Redirect'); //or Try for 4xx/5xx Code
header('Location: http://hello.php');
?>
预期结果
- HTTP/1.1 308 永久重定向
- 主机:localhost:8000
- 位置:hello.php
- 连接:关闭
- X-Powered-By:PHP/5.5.6
实际结果
- HTTP/1.1
302 Found
- 主机:localhost:8000
- 位置:hello.php
- 连接:关闭
- X-Powered-By:PHP/5.5.6
Apache 级别有问题还是 PHP 有问题?
更新:
最佳答案
根据 the function documentation page这是 PHP 在发送 Location header 时所需的行为。
编辑:抱歉,误读了您的消息。如果您发送不同于 3xx 的代码,它将覆盖响应代码,因此它应该与 308 一起使用。 您确定没有发回其他 HTTP 代码吗?您的客户端是否尝试检索新资源并且您看到 302 作为最终结果?
最终编辑:你试过了吗
header('Location: /some/page.php', TRUE, 308);
关于php - PHP 中的 header 会覆盖 HTTP 响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25379247/