PHP 301 Redirect, Impossible?

brant picture brant · Mar 11, 2011 · Viewed 10.7k times · Source

I have been trying to do a proper 301 redirect and have failed to do so. No matter what i try, its always a 302 redirect.

Returns a 302:

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

Returns a 302:

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

Can anyone explain why these are coming back as 302's and not 301's? Server OS is linux, running PHP/5.2.14. Try it yourself.

I will give you guys a URL to try. I am testing using YSlow and Googlebot.

Should be 301: http://www.fantasysp.com/player/mlb/Albert_Pujols/1486349

Firebug shows a 302 Code

Answer

Alix Axel picture Alix Axel · Mar 11, 2011

Pretty straightforward actually:

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

enter image description here


If you're using FastCGI try doing this instead:

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