Anyone tried making a website at least for once wanted to find a way to redirect his visitors to another place. Either his old page or an entirely new site. I did that when my new site was under development and it was redirecting them to my old one. And i even use this way now for my sites. I have 2 domains leading to the same exactly location but there this script directs each of the domains to its own folder having two total different sites into the same place.
Before we go with the script you should create the appropriate file.
- Create a new textfile named “index.php” (without quotes – notice the .php extension)
- open it with notepad and enter one of the below scripts (You must pay attention that the scripts must be placed on top of any other code or html tag into the source. I suggest you that if you have a redirection page do not have any other code into the file with this script, or if you want to have, place the script above everything even <html> tag
) - save it and place it into the root of your hosting space
Let’s see the script and explain it later on:
<?php
$domain = $_SERVER["HTTP_HOST"];
if (($domain == "siteA.com") ||
($domain == "www.siteA.com"))
{
header("location: http://www.mysites.com/siteA/index.html");
}
if (($domain == "siteB.com") ||
($domain == "www.siteB.com"))
{
header("location: http://www.mysites/siteB/index.html");
}
?>
Let’s see what’s happening here:
- The script gets the adress sent by the browser to the server and if the adress is “siteA.com” or “www.siteA.com” directs it to the folder “siteA” in “mysites.com” which is the main space. Now if the adress is “siteB.com” or “www.siteB.com” directs it to the “siteB” folder. This is the script i use now to direct my 2 domains (www.lambrospower.com and www.mastergenius.net )
If you have only one document, one space and just want to have a simple redirection from the root of your domain to any other place online you should use this code instead:
<?php
header("location: http://www.mysites.com/siteA/index.html");
?>
where the http://www.mysites.com/siteA/index.html is the place that your visitors will go visiting your domain. you can place anything you want there, google.com, the url to an image, the url to your facebook profile or even the adress of a file and when you visit your domain the download begins.
You must remember though that these scripts are entered at the top of your source before any html tag. The best way is to have a blank source with just these scripts into your root folder and remember to name it with the .php extension and be sure that your server supports PHP.
Thank you and for any questions or suggestions don’t hesitate to comment!!!

Need help on creating a website? Visit webmasterwizard.net for help!
Hello there, MG.net!
Thank you so much for this php redirection code.
I am a complete pre-newbie (yes below a newbie) to PHP!
I find your redirection code great, for example redirecting cheapgames.info to cheapgames.net.
However – I have a question. The domains do not seem to be coming across into google analytics as a referrer.
I was wondering is there anyway you can get this code to track as a referrer in google analytics?
I really would appreciate any help you could give on this.
Cheers, and thanks again for the site,
Alan