Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 301 Redirect, Impossible?

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

like image 818
brant Avatar asked Mar 11 '11 02:03

brant


People also ask

Why does 301 redirect not work?

The reasons for 301 redirect not working are much more well-defined among WordPress sites. One of the main causes is because you have added the rewrite rules on both the cPanel “Redirects” tool and from your WordPress plugin.

Is 301 a permanent redirect?

The Difference Between a 301 Redirect vs.301 redirects are permanent, whereas 302 redirects are temporary. A 301 is used when a page has permanently changed location, and a 302 should be used if you intend to move the page back under the original URL in the future.

How do I enable 301 redirect?

To 301 Redirect an Entire Domain: Replace http://www.new-domain.com/ with the URL that you would like your site to redirect to. Be sure to add a blank line at the end of your . htaccess document. This signals to the server that it has reached the end of the .


1 Answers

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);
like image 186
Alix Axel Avatar answered Sep 19 '22 17:09

Alix Axel