Redirect to same path on new domain

21,559

Solution 1

I will give you 2 functions which could be useful for some other thing;

function currentURL() {
     $pageURL = 'http';
     ($_SERVER["SERVER_PORT"] === 443) ? $pageURL .= "s" : '';
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return $pageURL;
 }


function redirect2NewDomain () {
    $url = currentURL();
    if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE) {
       return false;
    }
    # Get the url parts
    $parts = parse_url($url);
    Header( "Location : {$parts['scheme']}://{$parts['host']}" );
}

Ofcourse using .htaccess is much more easier and will be better for SEO;

RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L] 

I hope this helps

Solution 2

Something like this should work:

$uri = $_SERVER['REQUEST_URI'];
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://newsite.com$uri" ); 

But if you can modify your web server's configuration instead, that would be a better place to do it.

Solution 3

You shouldn't do this in PHP. These things can be easily done in your .htaccess:

#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomain.com$[OR]
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

This code will redirect olddomain.com/page.php to newdomain.com/page.php

It will also redirect folders: olddomain.com/folder/ to newdomain.com/folder/

By using this code google will also understand that you are switching domains and won't lower your page ranks for double content.

Solution 4

I like to use this code:

// BEGIN redirect domain
$domainRedirect = 'myNewDomain.com';
if(
($_SERVER['HTTP_HOST'] != $domainRedirect)
){
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://".$domainRedirect.$_SERVER['REQUEST_URI']);
exit;
}
// END redirect domain

this:

header("HTTP/1.1 301 Moved Permanently"); 

is optional but better for SEO: (https://moz.com/learn/seo/redirection)

Share:
21,559
Michael
Author by

Michael

Updated on July 15, 2022

Comments

  • Michael
    Michael almost 2 years

    Hi I've got a new domain and want to redirect my users to the new domain's equivalent path.

    So if they go on: oldsite.com/money.php?value=1

    Then it should direct them to: newsite.com/money.php?value=1

    I have the same header.php for all the pages, so can this be done with a simple php line?

  • Teneff
    Teneff about 13 years
    script_name doesn't contains the get variables
  • Revenant
    Revenant about 13 years
    I would definitely go with .htaccess method.
  • Emil Vikström
    Emil Vikström about 13 years
    You can send a 301 header in the PHP script and it will have similar effects from an SEO perspective.
  • Revenant
    Revenant about 13 years
    Yes, I could add that still it is not any better than .htaccess
  • Linus Jäderlund
    Linus Jäderlund about 12 years
    Just found this post because I had the same problem, went with .htaccess method. Worked great!
  • Jaider
    Jaider almost 8 years
    Thanks so much for the info .htaccess
  • Gabriele F.
    Gabriele F. over 7 years
    The .htaccess is the Solution, Thanks!