Rewriting URLs to host multiple domains on a single IP address with Apache

It's not absolutely necessary to purchase a separate hosting account and IP address for each of your domains. Instead, you can use one of Apache's less well-known features to host multiple domains on a single IP address.

If you're like most small Web site owners, you probably operate your Web site through a "shared" hosting account with a Web presence provider. Typically, this shared account gives you a unique IP address, a limited amount of disk space and bandwidth on the provider's server, and access to basic commands. Such accounts typically do not include super-user access to the system, nor do they allow you to install your own server programs.

Until recently, I operated under a common misconception in this regard: every time I purchased a new domain and set up a new Web site, I also purchased a new shared account from my Web hosting provider. Needless to say, this turned out to be an expensive proposition in annual subscription fees. It also wasn't optimal, because the disk space and bandwidth allowed to each account was almost never fully utilized.

In an effort to control my spiraling costs, I decided to do a little research to see if there wasn't a more efficient way of achieving my goals. And I found out something quite interesting: it's not absolutely necessary to purchase a separate hosting account and IP address for each of your domains. Instead, you can use one of Apache's less well-known features to host multiple domains on a single IP address, significantly reducing your expenditure on hosting fees. Keep reading, and I'll tell you how.

Step 1: Make sure your host uses the Apache Web server

Most Web hosting providers like the Apache Web Server, because it's stable, powerful and easy to configure. It's extremely likely that if your provider runs *NIX, your Web site is being served up by Apache. Check this with a quick email or call to your host's support department.

Two specific Apache features are required for the following steps:

  • The URL rewriting engine, which allows you to automatically reprocess URL requests according to pre-defined rulesets. This engine, mod_rewrite, is usually activated at compile-time, although it is also possible to activate it at run-time as a DSO module.
  • Per-directory .htaccess files, which allow you to override Apache's default configuration on a per-directory basis, through the use of special .htaccess files.

Check with your host's support department if these features are available. The following steps require both features to be active.

Step 2: Create a directory for each domain

In your primary account's public_html/ directory, create the following sub-directory structure:


Where site1, site2, site3 represent the various domains you wish to host. Populate each of these directories with the Web files for the corresponding domain.

Step 3: Apply rewriting rules

Once the directories are configured, create a file named .htaccess in the main public_html/ directory, and place the following line in it:

RewriteEngine On

This activates Apache's URL rewriting engine. Follow this line with rulesets like the one below:

RewriteCond %{HTTP_HOST}$ [NC]
RewriteCond %{REQUEST_URI} !^/vhosts/site1/.*$
RewriteRule ^(.*)$  /vhosts/site1/$1 [L]

RewriteCond %{HTTP_HOST}$ [NC]
RewriteCond %{REQUEST_URI} !^/vhosts/site2/.*$
RewriteRule ^(.*)$  /vhosts/site2/$1 [L]

The idea behind this is both simple and elegant. Whenever Apache encounters a request for, it automatically rewrites the URL and serves the contents of the directory /vhosts/site1. Requests for are similarly remapped to the directory /vhosts/site2, and so on. Since the rewriting takes place at the server end, most users will not even know this is happening.

Step 4: Purchase domain pointers from your Web host for the additional domains

The final step is to ask your Web host to publish the names of your various domains in its DNS zone files. This "domain pointer" is essentially a DNS entry that connects your various domains to your primary account's IP address; it can be purchased from most Web hosts for a nominal one-time fee.

After this is done, whenever a client requests one of your domains, the name server will respond with the IP address of your primary account. When the client attempts to contact that IP address, Apache will examine the domain name being requested and, based on the rewriting rules defined in the previous step, serve up the appropriate page from the domain directory.

If you have a number of small, medium-traffic Web sites, the above technique is a simple and efficient way to host them without excessive unnecessary expenditure. Try it for yourself and see!

Editor's Picks