views:

66

answers:

1

I posted this earlier

http://stackoverflow.com/questions/1915824/301-redirect-of-static-html-to-dynamic-php-page

But have a new idea, and am wondering if there are any issues why I should NOT do this...

If someone tries to go to a dead page on our site like:

(domain)/somepage.html

That now exists here:

(domain)/dynamic.php?id=1

It fails and goes to a custom Error 404 page (/404.php)

If I look at the $_SERVER['REDIRECT_URL'] variable, I can see where they were trying to go. My idea is to add an include at the top of the 404.php page to check this value, and if it's in my list of items to redirect, then to use PHP to do the 301.

Something like this...

// -- php include at top of 404.php page

switch(trim($_SERVER['REDIRECT_URL'])){
   case "/oldpage.html" : $location = "/dynamic.php?id=1"; break;
   case "/oldpage2.html" : $location = "/dynamic.php?id=2"; break;  
}

if(isset($location) && trim($location) != ''){
   header ('HTTP/1.1 301 Moved Permanently');
   header ('Location: '.$location);
   exit(0);
}

// -- end of php include

This gives me a single point to enter in all the links I see in the google webmaster tools that are in blog entries, etc. that are now dead.

Thanks

+1  A: 

Well, yes. 301, accompanied by a Location header, is the proper response for a request that you can positively identify as being moved.

troelskn