Wild Card Sub Domains

After a recent foreign top level domain registration binge aimed at some fun domain hacks, I got sick of spending money on foreign  TLDs, and started getting creative with subdomain hacks. TLD hacks are fun, but feel a little less natural to me, and certainly don’t tend to read as well; my eyes tend to stop at the TLD.

As it turns out, adding user customized subdomains to your applications is easier than I though. Follow the these three simple steps (you’ll need root access to your server in order to configure apache):

1) Create a new CNAME record for your domain with your registrar. You can do this by editing your DNS settings. Give your domain a name of “*” (astericks) and a value of your server’s ip address.

2) Open up your httpd.conf file and add in a virtual host container for your subdomain at the bottom. It should look something like this:

    DocumentRoot /path/to/your/app
    # probably something like /var/www/html/myapplication/
    ServerName  myapplication.com
    # your domain with no prefix
    ServerAlias *.sub.domain.tld
    # eg: *.bocoup.com

In redhat and centos, your httpd.conf file will be located in: /etc/httpd/conf/httpd.conf

It will usually be somewhere similar to that. Ubuntu and Debian do this in a completely different way, but still use the virtual host container construct.

Your httpd.conf conf file is similar to an .htaccess file. It is an Apache configuration file. In fact, you can put all of your .htaccess rewrite rules into httpd.conf, and they will work. The major difference is the rules in the httpd.conf file are loaded into memory when Apache starts, whereas rules in .htaccess files are read every time an http request is made to a resource that they affect.

In step one we set a wildcard CNAME record up at the registrar so that http://*.yourdomain.com (where * = anything) will resolve to the web server.

In step two, we added a virtual host container to tell apache to route all *.yourdomiain.com traffic to the same place on the server.

Any subdomain that is hit on your domain will now load the index file in the directory you specified as “myapplication” above.

3) Now we are set up to start building web applications that respond to wild card sub domains. You can simply parse the url from inside your application on the server or in the client, and respond conditionally based on subdomains, subsubdomains, subsubsubdomains, and so forth. You might consider setting up a routing file as the index to help manage your code, or, you might stick to a single page application.

This is one way to build wildcard subdomain applications. There are some other methods out there, but I have found this one to be the quickest for small projects.

Note: You need to have root access to your server to do this. Shared hosting providers often prevent this kind of setup or charge extra for it, since, among other reasons, this setup is ideal for the black hat search engine optimizers out there.


Contact Us

We'd love to hear from you. Get in touch!


201 South Street, Boston, MA 02111

New York

315 Church St, New York, NY 10013

Phone & Email

(617)379-2752 hello@bocoup.com