PHP 301 Redirect, Impossible?

2019-02-03 05:05发布

问题:

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

回答1:

Pretty straightforward actually:

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


If you're using FastCGI try doing this instead:

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