Quantcast
Viewing latest article 6
Browse Latest Browse All 10

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.

  1.  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4.     <head>
  5.         <meta httpequiv="Content-Type" content="text/html; charset=UTF-8">
  6.         <title></title>
  7.     </head>
  8.     <body>
  9.         <?php
  10.  
  11. switch($_SERVER['HTTP_HOST'])
  12. {
  13. case "www.internal.com" :
  14.  
  15. header( "HTTP/1.1 301 Moved Permanently" );
  16. header( "Status: 301 Moved Permanently" );
  17. header( "Location: http://www.internal.com/index1.php" );
  18. exit(0);
  19.  
  20. break;
  21.  
  22. case "www.freetutorialssubmit.com" :
  23. header( "HTTP/1.1 301 Moved Permanently" );
  24. header( "Status: 301 Moved Permanently" );
  25. header( "Location: http://www.freetutorialssubmit.com/index2.php" );
  26. exit(0);
  27.  
  28. break;
  29.  
  30. case "www.onlinehowto.net" :
  31. header( "HTTP/1.1 301 Moved Permanently" );
  32. header( "Status: 301 Moved Permanently" );
  33. header( "Location: http://www.onlinehowto.net/home/" );
  34. exit(0);
  35.  
  36. break;
  37.  
  38. }
  39.  
  40. // Here the code of your main domain continues. You can also redirect if you wish
  41. ?>
  42. </body>
  43. </html>

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!


Viewing latest article 6
Browse Latest Browse All 10

Trending Articles