php - PHP 中的 header 会覆盖 HTTP 响应代码

标签 php apache http http-headers

在 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 有问题?

更新:ScreenShot

最佳答案

根据 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/

相关文章:

ajax - 飞行前 OPTIONS 请求通过 HTTPS 失败

django - 尝试编写只读数据库 Django/Apache2/OperationalError

javascript - 使用 javascript 或 php 在所有子域的链接中将 Https 更改为 Http

php - 如何在cPanel Hosting中像Wamp一样设置对 "Allow from all "的权限

javascript - php jwt 登录存储

php - 多选 MySQL 数据中的复选框,用逗号 (,) 分隔

php - 使用 PHP 将网站连接到 Azure SQL 数据库

phpMyAdmin 和 mysql 未显示相同的用户或数据库

HTTP 响应代码为 403 而不是 200,为什么?

php - Laravel 5.1 - 显示存储在存储文件夹中的图像