Saturday, March 17, 2012

Redirecting based on the URL string using PHP

In many cases, one ends up hosting multiple sites on a single server, shared or VPS.

Also in many other cases a company has multiple domains and multiple page addresses to service clients and prospects from different countries, demographics and walks of life.

And the need of the hour is that one wants to send a user to a different folder on your web site, based on what domain they use? Here we will explain a simple way to do this using PHP.

For instance on my VPS, I have 2 domains pointing: one domain called sampleno1.com and the other one called sampleno2.com. Both point to the IP of the VPS.
We have an index.php file in the root of the web server on the VPS containing the following code.

<?php

if (($_SERVER['HTTP_HOST'] == “sampleno1.com”) || ($_SERVER['HTTP_HOST'] == “www.sampleno1.com”))
{
    header("location: folderforsampleno1");
}

if (($_SERVER['HTTP_HOST'] == “sampleno2.com”) || ($_SERVER['HTTP_HOST'] == “www.sampleno2.com”))
{
    header("location: folderforsampleno2");
}

?>


The code asks the PHP server to see what the HTTP_HOST header is. If it is sampleno1.com or www.sampleno1.com, then it redirects to the folder on the server which contains the code for the site sampleno1.

If it is sampleno2.com or www.sampleno2.com, then it redirects to the folder on the server which contains the code for the site sampleno2

No comments:

Post a Comment