Host multiple domains on one hosting – cheating the hosting companies.

I barely remember my first site and how it was created, but I still remember the filling when I saw the price for the webhosting I was searching for. Then I was a student and if I wanted to see my website live, I had to pay one third of my monthly budget. That was before more than ten years and the webhosting was pretty expensive than now.

Nowadays the hosting prices are way to low, but if you are familiar with webhosting and understand the services that some hosting companies provide – you probably know that even price of couple of bugs is too much for what you actually get. I am not going to fail into detailed discussion about hosting companies, plans etc.- so if you are at the point where you do not have much to spend on webhosting, and in the same time you want to start building websites and host them cheep you may use the method described bellow.

Lets consider the following case:
You have bought webhosting which include something like this:
One domain hosted, 160GB drive space, MySQL database, PHP, any-other-marketing-bla-bla . (For Windows hosting users, please read Redirect domain alias to a particular folder in Windows)

Certainly the main point here is that you have pretty much space, PHP and MySQL which allows you to do almost everything, and .. just one domain hosted. Here I have to say that the whole freetutorialssubmit.com website is not more than 600MB and if I decide to host it on such hosting, the rest of the 160GB space will remain unused. You may decide to use the rest of the space as a storage, but in most cases it just remain like that.

With the PHP script bellow you can host multiple domains on one hosting even it the hosting company allows only one domain to be hosted, which will allow you to use the rest of the hosting space for different websites.

Before that I have to mention that all domain you want hosted in this hosting should be set like CNAME for the main domain. The reason is that when a domain is set as a A CNAME record or Canonical Name record in the Domain Name System (DNS) – this specifies that the domain name is an alias of the other domain. This is rather simple and can be done in with the company where the domain is registered for free. (If not try GoDaddy ;)

Once a domain is set as CNAME it will do exactly the same as the first domain. Here comes my script which basically check as which domain the browser request is done and redirects to a particular folder where the according domain files are set. The script should be in the main index.php file, otherwise it will not be opened and will not redirect accordingly.

Ok, it may looks confusing, but what is actually happening is when someone lands on your hosting, his browser opens the main index file. The script checks ‘HTTP_HOST’ global PHP variable which contains actually via which domain the server was reached and then redirects to its index (you can see it in the example as index2.php and index1.php ), or to a particular folder. If there is no match the visitor’s browser will stay in the same file and will continue with the main domain code.

Well that’s it … and yes, it is legal to host multiple domains on one hosting using this way!

Filed Under: PHPWeb serversWeb-hosting

Anthony Gee About the Author: Anthony G. is an IT specialist with more than 9 years of solid working experience in the Web Hosting industry. Currently works as server support administrator, involved in consultative discussions about Web Hosting and server administration. One of the first writers in the Onlinehowto.net website, now writing for Free Tutorials community - he is publishing tutorials and articles for the wide public, as well as specific technical solutions.

Comments (2)

  1. Question says:

    Wy don’t you use .htaccess instead?

  2. Anthony Gee Anthony Gee says:

    Well, the idea is when AllowOverride function is not allowed.

Leave a Reply