Running multiple sites on shared hosting (with some DNS and HTACCESS magic)

The worlds network
Plugged (Image by saschaaa)

I recently had the need to set up several different websites in one (hosted, shared) webspace. While this by itself is trivial there was a catch: I had multiple domains that needed to be DNS forwarded (using CNAME) to this webspace. In essence, the functionality I needed was that once a user goes to one of those domains, he/she would get the site served from one of the subdirectories in my webspace.

This again were trivial if the domains had been hosted with the company that provides me with the webspace. But in my case, the domains could not be moved and therefore I needed the DNS forwarding. In case you run into a similar situation, here’s what you need to do:

  1. Make sure you have Linux/Apache.
    This method works with Linux and Apache. I don’t know how to do this with Windows since IIS doesn’t have a clean way to rewrite URLs.
  2. Get a static IP.
    This is usually not included in a cheap, shared hosting plan. Some hosters offer those but mine didn’t. However, with my hoster (, I was able to purchase an SSL certificate, which gave me a dedicated IP.
  3. Set up subdirectories and add URL rewriting
    Set up one subdirectory per site and add the site content into these. Then add a .htaccess file into the root of the webspace and add the following lines. Of course you’ll have to modify them to your need.
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
# Site 1
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteCond %{REQUEST_URI} !^/subdirectory1/.*$
RewriteRule ^(.*)$ /subdirectory1/$1 [L]
# Site 2
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteCond %{REQUEST_URI} !^/subdirectory2/.*$
RewriteRule ^(.*)$ /subdirectory2/$1 [L]


Comments and Reactions