Hosting Tricks: Hosting Unlimited Number of Domains in One-Domain Hosting Account
6 Mar2006

Some times we need to use our existing hosting account to (maybe temporarily) place another web-site in it. But what we can do, if our hosting provider allows only one hosting directory and only aliases for main site (as GoDaddy.com does)? We can use the following Apache+mod_rewrite trick to host unlimited number of domains on one hosting directory.

First of all, we need to point our new domain to hosting server IP. If server’s IP is static, we can do it by simple A-record in our DNS-zone control panel:

    new-domain.com  IN A IP.ADD.RE.SS

If you don’t know IP address of hosting server or this address is not permanent (for example, because of some load balancing used by hosting provider), you can use simple trick with CNAME-record in your new DNS-zone:

     new-domain.com  IN CNAME already-hosted-domain.com.

After the first step was finished we have new-domain.com pointed to our hosting provider’s server. Now, we need to add this domain support to hosting server. We can do it by your hosting provider’s “Domain aliases” option or another option with such meaning.

After we have associated our new domain name with existing directory on hosting server (/hosting/dir), everything we need is to do something to force hosting server to use some sub-directory for all requests to new-domain.com (/hosting/dir/new-domain). To do it, we need to put following code into the .htaccess file in /hosting/dir directory:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^/new-domain/
    RewriteCond %{HTTP_HOST} new-domain\.com$
    RewriteRule (.*) http://already-hosted-domain.com/new-domain/$1 [L]

That’s all! After we have created this file, all requests to new-domain.com will be pointed to /hosting/dir/new-domain directory.