Setting up wildcard subdomains on Apache

Submitted by Rollie Hawk on Fri, 2005-11-25 17:08.

Wildcard subdomains are useful for a variety of reasons. If you want to host blogs or forums for multiple users, they are handy because they can get username.domain.tld working no matter what username is without any administrative work. It can also be used by content management platforms like Drupal to handle multiple subdomains with a single installation. If nothing else, it can be used to make sure links that start with misspellings like ww and wwww go where they were intended to.

So how does one set this up?

For web servers running Apache, this is essentially a two step process. If you look around message boards, you'll find quite a few people getting one part of the process right but missing the other.

Creating a wildcard DNS record

The first step is to create a wildcard DNS record. This is important because while your DNS server is already telling visitors where to find domain.tld, it doesn't know where to tell them to find subdomain1.domain.tld.

To do this, you'll need to create what is called an "A record," which is short for "address record." As the name implies, "A records" tell what IP address a host is pointing to.

How this is done varies according to your DNS server and what control panel (or command line) you are using, but it will be very similar. All you have to do is create a record of type A pointing from *.domain.tld to your web server's IP address.

If you are using a control panel, you can probably set this using a web form. You may have to get your web host to do this if this functionality isn't built in.

If you are making these changes manually, it will depend on your DNS daemon. or example, if you are using BIND, you will need to add the line below to domain.tld.db.

*.domain.tld. 14400 IN A web.server.ip.address

Again depending on your DNS server, you may need to restart your web server's DNS service. Either way, you'll need to be patient because DNS settings take some time to propagate throughout the Internet (expect at least a few hours for this change to take effect).

Editing your virtual host settings

Creating the DNS record above tells the world what web server to look for your wildcard subdomains on, but how does the web server know where to send them? This is where Apache's virtual host settings help out.

Unlike the previous step, there is a good chance that you'll have to make this change manually (or have your web host do it for you). In domain.tld.conf, you want to look for .

Then look for something like ServerAlias ftp.domain.tld mail.domain.tld. If it's there, add *.domain.tld to the end of that line. If it isn't, just add a new line with ServerAlias *.domain.tld.

Once you have made this change, go ahead and restart Apache.

Conclusion

After a few hours, your new DNS settings should propagate and your wildcard subdomains will be ready to use. Best of all, once you set this up, you won't have to wait for your new subdomains to propogate in the future.


( categories: Articles | Tutorials | Web Development )
Rollie Hawk is a consultant, web publisher, online personality, magazine writer, web developer, network administrator, teacher, husband and father residing in southern Illinois. He graduated in 2002 from Southern Illinois University, earning his BS majoring in math with a minor in chemistry.

Rollie is a certified math teacher with endorsements in chemistry, physics, and physical science and has taught students of all age groups and abilities, ranging from grade school to the university level. In addition to math and science, he has also taught GED, job skills, and alternative high school classes (his personal favorite).

After the birth of his daughter in 2004, Rollie decided to spend more time at home. This meant leaving his teaching position and devoting his working hours exclusively to consulting, web development, and general IT work.