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 (1and1.com), 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} ^sub1.site.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdirectory1/.*$
RewriteRule ^(.*)$ /subdirectory1/$1 [L]
# Site 2
RewriteCond %{HTTP_HOST} ^sub2.site.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdirectory2/.*$
RewriteRule ^(.*)$ /subdirectory2/$1 [L]

 

  • Dreamhost is giving me a hard time anyone has a suggestion for a better host?

  • Ca Stor

    What happen if I have multiple domains (domain.com, domain.dk, domain.eu) pointed to same flolder

    • If you want to keep the domains intact in the URLs, then you don’t have to do anything. Just use the DNS to point at the folder.

      If you want them all to end up as e.g. “domain.com”, then use something like this:

      RewriteCond %{HTTP_HOST} ^domain.net$ [NC]
      RewriteRule ^(.*)$ http://domain.com/$1 [L]

  • How would one go about if one had 2 sites on the server, one in the root and one in a sub folder?

  • Do you know of anyway with htaccess to disable someone from using your domain to point to their own website on the same server? Ex: they use YOURDOMAIN.com to promote their PHISHING WEBSITE.COM by using this simple URL to send users : YOURDOMAIN.COM/~phishing/file.html

    Any help would be greatly appreciated. Thanks

    • Don't use my method where multiple people (and potentially some bad ones) have access to the individual installation folders (e.g. yourdomain.com/~site2/). For that scenario, use the apache (or IIS) settings to create multiple sites. Then it would be impossible for anyone to access one domain via another.

  • Although shared hosting is a less expensive way for businesses to create a Web presence, it is usually not sufficient for Web sites with high traffic. These sites need a dedicated Web server, either provided by a Web hosting service or maintained in-house. With shared hosting, numerous web sites are sharing a single server.