Search code examples
dnssubdomainon-the-fly

Creating subdomains on the fly


When one signs up for Blogger or WordPress, one gets their very own sub-domain that works instantly. How can I achieve the same, given that I have my own VPS/VDS/Dedicated server?


Solution

  • In a nutshell:

    1. Create a wildcard domain in DNS (i.e., resolving whatever.yourdomain.example returns your IP),
    2. create a default virtual host in your web server and
    3. check the URL in your application.

    How to do this depends on what technology you use. Let me give you some examples:

    1. How to set up a wildcard domain in BIND and in Windows Server DNS.
    2. To create a default virtual host, you just need to create a web server without a host entry in IIS. In Apache, the first virtual host listed in the configuration file becomes the default host.
    3. Here, you can either (a) rewrite the URL depending on the domain (i.e., converting the subdomain into a parameter in the URL, example for ASP.NET, examples for Apache with mod_rewrite: Link1, Link2), or (b) just have a look at the host part of the URL (e.g. Request.Url in ASP.NET).

    Addition by bortzmeyer (sorry for overwriting your edit, there was an edit conflict):

    The syntax for a wildcard, in the usual DNS zone file format (described in RFC 1035 and implemented in BIND, nsd and may be others) is with a star:

     *   IN    A   198.51.100.3