How to Dynamically Create Sub Domains

Have you even seen a website that immediatly assigns you a personal subdomain as soon as you sign up?  Sometimes it takes the form of: or something like that.  This article will show to how to produce that effect so you can offer this feature to your users.

I used this particular feature to offer new groups on my website their own group subdomain. Groupbox is a website for online groups.  So if you went to Groupbox and created a new group, you could also choose a unique subdomain to quickly navigate to your newly created group. It would look something like

After reading this guide you will be able to:

Dynamically create subdomains and use them to send users to specific parts of your website.

What you will need:

I’ll need to assume you know a bit about programming on the web, the .NET framework, and in this case some VB.NET.

This guide references websites that are hosted on IIS. If you’re using something else then you can probably still just adapt most of the logic to suit your hosting.

You will probably need to make a change to your DNS zone file for your domain.  We’ll discuss this later.

You will need to use ISAPI_Rewrite (a 3rd party product) to implement this solution. The lite version is free and will suit your needs.  It is installed on the server and uses regular expressions to manipulate URLs. More about this later.

How we are going to do it (Summary)

To achieve the effect of dynamically creating subdomains what we need to:

  • redirect all requests sent to the server with a subdomain to a specific page
  • somehow let that page know which subdomain was used
  • then redirect the user to the specific page their looking for

To do this, we’ll need to do 3 steps.

Step 1: Setup DNS

You will need to create a catchall or wildcard in your DNS zone file so that it will process any requests with a subdomin like they are normal web pages.  By adding the following refernce to your DNS you can achieve this:

* 14400 A

Where the IP address is your webserver IP.  Please note that many DNS are different and that you may not even have direct access to make changes to your zone files. Just contact your hosting company and explain you want to create the above effect and they should be able to do this for you.

Step 2: Configure IIS

You’ll need to let IIS know that you are now sending all sorts of requests for webpages with different domain name host headers.  If this sounds technical then don’t worry – it is easy.  Using IIS, open the properties for your website. Under the ‘Web Site’ tab click ‘Advanced’ next to the IP Address field.  Remove whatever is in the hostheaded field so that it only shows an IP address and a port.

It is possible that you don’t have access to IIS on the server your website is on. Contact your webhosting company and let them know what you want to do.  If they can’t do it because you’re in a shared hosting environment then you might want to consider upgrading to Virtual Private Server hosting which will give you this increased control.

Step 3: ISAPI Rewrite

Here comes the cool bit. Now that you have told your DNS to send any requests with any subdomain to your webserver (Step 1). And you’ve told your webserver IIS that any request with an unspecificed subdomain should just go to your primary website (Step 2), we can now interpret these requests and handle them appropriatly .

ISAPI Rewrite does exactly this. It allows you to intercept URL requests and manipulate them. What we will do is take the part in front of your domain, the subdomain part of the domain, and append it to a querystring that you can then interpret.

Here is an example of what I mean:

Incoming request:

Rewrites to:

The expression you need to use for ISAPI Rewrite is as follows:

RewriteEngine On

RewriteCond %{HTTPS} (on)?

RewriteCond %{HTTP:Host} ^(?:www.)(.+)$ [NC]

RewriteCond %{REQUEST_URI} (.+)

RewriteRule .? http(?%1s)://%2%3 [R=301,L]

RewriteCond %{HTTP_Host} ([^.]+)$ [NC]

RewriteRule (.*)$1?username =%1 [NC,L]

This may look meaningless to you but once you read a little bit about ISAPI Rewrite, you’ll quickly see where to put this code and you’ll be away.

Please take note that this will also remove the www from the front of all of your domain name requests. If you don’t want this to happen then only use the first line (to turn the engine on) and the last two lines.

Step 4: Handling the querystring

It should be obvious to you what to do now: you just need to create a page that will look at the querystring parameter and then send the user to anywhere you want, in this case it would lookup the username in your website database and send the user to their profile page. I haven’t been very detailed on this step because it will be different in every case but if you have problems then please contact me and I’ll help you.


This is quite a professional looking feature once you implement it. In some cases it may not be possible to do because of limitations on how much you can configure a shared hosting environment. Good luck.

Article Source:

About the Author: Will Max D is a developer at Groupbox. Groupbox is a website for online groups. (

Disclosure: Some of our articles may contain affiliate links; this means each time you make a purchase, we get a small commission. However, the input we produce is reliable; we always handpick and review all information before publishing it on our website. We can ensure you will always get genuine as well as valuable knowledge and resources.

Related Articles You May Like:

Article Published by Souvik Banerjee

Souvik BanerjeeWeb Developer & SEO Specialist with 10+ years of experience in Open Source Web Development, specialized in Joomla & WordPress development. He is also the moderator of this blog "RS Web Solutions".

Leave a Comment

1 thought on “How to Dynamically Create Sub Domains