Programming is an art, Bridging between technically and asthetically.

Bookmark This Page · Follow me at Twitter! · Become my Friend at Facebook · My Google+

Wildcard hosting with Apache and Bind

Wildcard hosting with Apache and Bind
If you have control over your DNS you can setup 'wildcard hosting', which means you can have a webserver accept connections for any given subdomain. This can be enormously useful for community websites, or other hosting purposes.
One popular site which uses this technique is Livejournal, another less obvious one is
The end goal of using wildcard hosting is that you'll be able to setup a webserver for a domain, such as which will serve requests for any subdomain. For example the following would all be valid hosts:
* some.really.really.long.example.
You can even have each domain point to different pages if you use mod_rewrite. This kind of hosting can be setup fairly easily, with just a couple of small steps:
  1. Setup your DNS entries for the domain you wish to use.
  2. Setup Apache, or Apache2, to handle the domains.
  3. Optionally use mod_rewrite to point particular domains at different content.

Wildcard DNS

The setup of wildcard DNS is something that should be simple if you have control over the DNS server(s) which are in use for your domains.
If you use your domain-resellors control panel there may be an option for it, if not you might need to have somebody else host your DNS for you. (I'll do it for a fee ;)
Using the common bind package you can setup wildcard entries by the addition of a record with the name "*".
Here is an example of the entry you'll need to add:
*    IN      A
Add this to the bottom of your zone file taking care that you add it beneath any static subdomain entries you might have. Here is a full example: 86400     IN      SOA (
                                        2005100804      ; Serial YYYYMMDDXX
                                        10800           ; Refresh
                                        3600            ; Retry
                                        3600000         ; Expire
                                        86400 )         ; minimum
                        IN      NS
                        IN      NS
                        IN      MX      10
                        IN      A
mail                    IN      A
ns1                     IN      A
ns2                     IN      A
*          IN      A
Here we see the wildcard entry at the bottom which causes any query of * to return the address (except for those static ones ns1, ns2, and mail in this example).
Note that this will mean your DNS server will receive more traffic.

Apache Setup

Once you have your DNS entries setup you can alter your Apache virtual hosts to add an appropriate ServerAlias entry. This will allow Apache to treat a number of hostnames as equivilent.
A typical VirtualHost would look something like this:
<VirtualHost *>

        DirectoryIndex index.html
        DocumentRoot /home/www/

To allow this server to serve multiple other valid hostnames we simply add a ServerAlias setting:
<VirtualHost *>
    ServerAlias *

        DirectoryIndex index.html
        DocumentRoot /home/www/

This will tell Apache that any host with the suffix will match this virtual host too.

mod_rewrite setup

With a setup as described so far you'll end up with a server which is capable of serving the same contents to clients, regardless of the hostname used to connect. Frequently it is useful to send clients to a different location, and this is something that mod_rewrite can handle.
Assuming that you're using Apache2 you can enable mod_rewrite by running: a2enmod rewrite
(If mod_rewrite is new to you then you should refer to our earlier article mod_rewrite for Apache and Apache2 under Debian.)
As an example we'll consider a site which uses several subdomains:
These sites should each point to a particular set of contents:
  • should point to
  • should point to
  • should point to
To do this we can use mod_rewrite to match against the Host: header that client browsers will send. The following rules will do the job:
RewriteEngine on
RewriteCond %{http_host} .
RewriteCond %{http_host} !^ [NC]
RewriteCond %{http_host} ^([^.]+)\ [NC]
RewriteRule ^(.*) [R=301,L,QSA]
Note that with this setup a request for will redirect visitors to - which most likely won't exist. So make sure you have a good error page!
Taking these lines one by one we can see how they work. The first line simply enables the use of the mod_rewrite engine.
The second line instruct the module that we wish to match against the http_host, or Host:, header.
In the third line we test that the requested host is not If that is true then the fourth line wil determine the hostname used in the request and store the part before in a variable "%1".
The last line simply redirects visitors to a new path with the result of this capture.
Format Lainnya : PDF | Google Docs | English Version
Diposting pada : Selasa, 22 Juni 10 - 12:26 WIB
Dibaca sebanyak : 5527 Kali
amarullzamarullz · Test Komentar Berguling di lantai 
Diposting pada : Kamis, 24 Juni 10 - 08:52 WIB
amarullzamarullz · These sites should each point to a particular set of contents:
* should point to
* should point to
Diposting pada : Kamis, 24 Juni 10 - 09:09 WIB
amarullzamarullz · The last line simply redirects visitors to a new path with the result of this capture.
Diposting pada : Kamis, 24 Juni 10 - 09:10 WIB
Anda harus Login terlebih dahulu untuk mengirim komentar
Facebook Feedback