2011-03-11 84 views
15

我一直在試圖做一個正確的301重定向,並沒有這樣做。無論我嘗試什麼,它總是一個302重定向。PHP 301重定向,不可能?

返回302:

http_redirect("urlgoeshere", '', false, HTTP_REDIRECT_PERM) 

返回302:

header("HTTP/1.1 301 Moved Permanently"); 
header("Location: urlgoeshere"); 

任何人都可以解釋爲什麼這些都回來爲302的,而不是301的?服務器操作系統是Linux,運行PHP/5.2.14。親自嘗試一下。

我會給你們一個URL來嘗試。我正在使用YSlow和Googlebot進行測試。

應該是301:http://www.fantasysp.com/player/mlb/Albert_Pujols/1486349

Firebug shows a 302 Code

+0

適用於我:'$ curl http://localhost/test.php -i'→'HTTP/1.1 301永久移動我們需要更多關於您的環境的信息。 – deceze 2011-03-11 02:57:20

+0

你在Windows,Linux或Mac上嗎?顯然IIS有一些問題... – 2011-03-11 03:16:27

+0

可能重複的[實際上做了302重定向的php 301重定向](http://stackoverflow.com/questions/1562243/php-301-redirects-actually-doing-a-302-redirect ) – 2011-03-11 03:40:43

回答

31

非常簡單實際:

header('Location: ' . $url, true, 301); 

enter image description here


如果你使用FastCGI嘗試做這個:

header('Status: 301 Moved Permanently', true); 
header('Location: ' . $url); // or header('Location: ' . $url, true, 301); 
+0

我試過了,它仍然無法工作。我將網址添加到爲我顯示302的實時頁面。你也看到302了嗎? – brant 2011-03-11 03:04:01

+0

@brant:我看到301.你用什麼來檢查HTTP響應代碼? – 2011-03-11 03:06:53

+0

有趣。我在Firefox中使用YSlow進行測試,並在網站管理員工具中查看Googlebot的回覆。兩者都爲我顯示302。 – brant 2011-03-11 03:17:11