簡體   English   中英

為什么http_response_code()不起作用?

[英]Why does http_response_code() not work?

我使用的是PHP 5.4.35。

這段代碼:

<?php
http_response_code(404);
header('Location: /');
?>

使我的服務器返回以下答案:

HTTP/1.1 302 Moved Temporarily
Date: Sat, 20 Dec 2014 15:06:32 GMT
Server: Apache
X-Powered-By: PHP/5.4.35
Location: /
Content-Length: 0
Keep-Alive: timeout=2, max=200
Connection: Keep-Alive
Content-Type: text/html

為什么看不到404


編輯:

根據答案, Location覆蓋404狀態代碼。 由於被調用的頁面不是臨時移動,而是根本不存在,因此我現在提供帶有http_response_code(301)301 Moved Permanently狀態代碼,該代碼與Location header結合使用。

根據文檔,如果您向下發送位置標頭,則如果它不是201或3xx狀態代碼,則會導致302。

http://php.net/manual/zh/function.header.php

您正在發送一個Location標頭,該標頭將404 Not Found302 Found / Move Temporarily標頭一起覆蓋。

重定向用戶而不顯示404毫無意義。 我建議您發送404錯誤代碼,以便搜索引擎知道該頁面不再存在(並刪除重定向)。

並顯示指向首頁的可點擊鏈接(並顯示一個站點搜索框),以便錯誤頁面仍可供人類訪問者使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM