{steinsoft.net}
 
 
 
 
 
Home/News
Code Snippets
Board
Projects
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Leipzig
Home :: programming :: articles :: Apache & Wildcard Subdomains

[ Apache & Wildcard Subdomains ]

In this short article I will show you how to setup something really interseting and useful: configuring a virtualhost with the Apache webserver that accepts wildcard(*) domain names. This is a really nice features which can be useful in many cases.

Say you have a website and want to access sites by using a subdomain syntax like article1.programming.domain.tld. One way for this is to setup a virtualhost in the Apache configuration file that points or redirects to the corresponding page. Problem of this solution: it's simply _stupid_ as you would have to setup a virtualhost for every single page. The better way: make your apache accept any subdomain of that domain - *.domain.tld - and write a script that dynamically chooses the right page e.g. using a script language like PHP.

Domain setup?

You can skip this short section if you have already setup your DNS server to allow wildcard subdomains. If this is done, all subdomains are directed to the IP address you specified but you will still have to setup the webserver to manage those hostnames.

I won't go into detail here as DNS server configuration varies depending on what server you are actually using. But I will show how easy it is to get this features working with one of the most popular DNS servers, bind.

To enable wildcard subdomains, you only have to add one single line to you domainname zone:

...

; wildcard subdomains are all directed to this IP
;  of course this should be the IP of your web server
*.domain.tld.      IN  A    1.2.3.4

...

If you are using some sort of other DNS server, please check the documentation.

Setup in Apache

I assume you have a general understanding of the Apache configuration file and especially of how to setup virtual hosts. So to get quickly to the point, here is what you have to add to your exisiting httpd.conf configuration file.

...

#
# Your VirtualHosts section
#

NameVirtualHost 1.2.3.4


##
# this one accepts any subdomain
##
<VirtualHost 111.22.33.55>
    DocumentRoot /www/subdomain
    ServerName www.domain.tld
    ServerAlias *.domain.tld
</VirtualHost>

...

I think there isn't much explaination needed here. As you see in this little snippet, setting up a wildcard virtualhost is pretty much like setting up the DNS server.

If there are still questions left, check the message board. Or if you really can't avoid it, ask me.. {grin}. Happy apache'ing!

 Last edited 2005-04-16 21:32:54 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0