3

I want to 301 a large amount of old URLs.

Can I do this via PHP in a 404 script, or would this mess up search engine results (them thinking the pages are not found)?

My current .htaccess:

ErrorDocument 404 /404/

My 404/index.php script plan:

<?
switch (rtrim($_SERVER["REQUEST_URI"],"/")) {
    case "/blah/foo" :
        $redirect="/somewhere_else/";
        break;
    case "/over_here" :
        $redirect="/over_there/";
        break;
    case "/etc/etc/etc" :
        $redirect="/etc/and_so_on/";
        break;
}

if($redirect) {
    header ("HTTP/1.1 301 Moved Permanently");
    header ("Location: ".$redirect);
    exit();
}
?>
<html>
    <head>
        <title>404: Page not found.</title>
    </head>
    <body>
        <h1>404: Page not found.</h1>
    </body>
</html>

So, basically, the redirecting isn't a problem, but I don't want a search engine to think these pages are "not found" or will "think less" of them, or anything like that...

Is this legit? Will Google be happy with this?

PTG
  • 31
  • 1

1 Answers1

0

Sending a Location HTTP header automatically sets status accordingly. It's documented and it's easy to test with Firebug or your browser's equivalent tool.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • You need to add header("HTTP/1.0 404 Not Found"); – Nanhe Kumar Apr 01 '13 at 09:43
  • @NanheKumar - The whole point of the question is avoiding 404. You can only have one HTTP status and it needs to be 301. – Álvaro González Apr 01 '13 at 09:45
  • You are right but you can see above code if $redirect is not set then you are working for 404 – Nanhe Kumar Apr 01 '13 at 10:05
  • @NanheKumar - Sorry but I can't understand what you mean. The question is IMHO quite clear: use Apache's Not Found handler page to redirect to moved documents when possible. I think it's possible and I've even done it myself for years. – Álvaro González Apr 01 '13 at 10:07
  • I just tried this out, using "Fetch as Google" (what a nice tool!). It appears to work nicely! – PTG Apr 01 '13 at 11:34