Home > Programming > Forwarding a PHP website from one domain to another.

Forwarding a PHP website from one domain to another.

December 28th, 2009 Jimmy Leave a comment Go to comments

Forwarding a PHP website from one domain to another.If you are planning on moving a website from one domain name to another you’ll probably want to forward all your traffic from your old site. Many people just forward the actual domain name to point to the new domain name but this won’t help people who land on specific pages. If your links stay the same and it’s just a domain name change this can be done with just a few lines of code. If your linking structure has changed, you’ll need to do some additional work. Below is the code for a domain name change where the new site links are the same as the old site links with the obvious exception for the domain name.

This would be a good example of what you could do if you were running a CMS or Blogging system such as PHPNuke or Wordpress. This code checks to see which page is being requested and then builds a new link to forward the user to. It also lets any search engines know your page has moved with the 301 header tag.

<?php
$uri = $_SERVER['REQUEST_URI'];  //Get the incoming URL request not including the domain name....
header("HTTP/1.1 301 Moved Permanently"); //Tell the requester this page has moved.
header('Location: http://www.new-website.com' . $uri);  //Concatenate the requested URL onto our new domain name and forward the requester.
die?>

If your new site contains a different linking structure than your old site you may want to forward your most popular pages to the newly formatted URLs.

<?php
$uri = $_SERVER['REQUEST_URI'];  //Get the incoming URL request not including the domain name....

$oldSiteLinks =  array("/contacts.html","/products.html","/chatforums/");
$newSiteLinks = array("http://www.newsite.com/aboutus.php","http://www.newsite.com/newproducts.php","http://forums.newsite.com");

//Make sure the two above array are aligned. Each element should be alligned to the corrisponding "oldUrl / newUrl" format.
//Element 0 in oldSiteLinks should forward to element 0 in newSiteLinks!

if(in_array($uri,$oldSiteLinks))
{
  //If the requested URL on the old site is one of the links I wanted to forward ($oldSiteLinks) then lets redirect that page...
  $idx = array_search($uri, $oldSiteLinks); //get the index for the oldsite link.
  $newURL = $newSiteLinks[$idx]; //Get the new link from the corresponding link array.
}else{
  //If we don't find a page in the $oldSiteLinks array just forward them to the new domain.
  $newUrl = "http://www.new-site.com";
}

header("HTTP/1.1 301 Moved Permanently"); //Tell the requester this page has moved.
header('Location:  $newURL);  //Redirect the requester to the new URL. The newSiteLinks array items contain the domain name so no concatenation is required.

There are of course more ways you can do this however this has been the easiest method I’ve come up with. Moving to a new domain name and new site all together with all new links is always challenging but with a few minutes of work you can at least forward your popular pages and sections of your old site to your new one.

Categories: Programming Tags:
Digg: DIGG ME
  1. No comments yet.
  1. No trackbacks yet.