Permanent Redirection with HTTP 301 using PHP

When you change your existing website on a new domain, you would be thinking to redirect all pages with their respective pages on a new domain.

For example, you would be needing.
olddomain.com/page.php => newdomain.com/page.php

When someone opens olddomain.com/page.php, they should be automatically redirect to newdomain.com/page.php. This thing has 2 benefits.

1. Search engines like Google easily find that your website has moved to a new domain.
2. It is convenient for visitors to read the same content if they have bookmarked your old website url.

Furthermore, if you have more than one domains for a same website (most probably for the corporate websites, you might have booked all famous extensions such as .com, .net, .org, etc)

$httphost = $_SERVER[‘HTTP_HOST’];
$scriptname= $_SERVER[‘REQUEST_URI’];
$tourl = “http://www.newdomain.com” . $scriptname;
if($httphost != “olddomain.com” && $httphost != “www.olddomain.com”)
{
header(“HTTP/1.1 301 Moved Permanently”);
┬áheader(“location: ” . $tourl, true);
}


Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks

Leave a Reply

Your email address will not be published. Required fields are marked *