Do you have a web site that is hosted on a Windows Server that has been growing and now it makes more sense to redefine the architecture and separate that web site into subdomains? In this article, we will be discussing two methods of setting this up. And later in the article, we'll provide you with a comparison analysis and our conclusions.
You have two options. Since you probably have a limited number of subdomains to manage, you can either list them out individually or just use a wildcard.
Wildcard method: Add the following entry into your DNS server and change the domain and IP address accordingly.
*.example.com IN A 22.214.171.124
Declare manually: Add an entry for each of the subdomains.
sub1.example.com IN A 126.96.36.199
sub2.example.com IN A 188.8.131.52
sub3.example.com IN A 184.108.40.206
First make sure you have created a root directory for each of the subdomains, as if you are working with a new web site. Your directory might look like the following:
Next, we create a web site for each of the subdomains. Let's create the first one and you can repeat this for all others.
What we have accomplished here is we have separated your web site into multiple subdomains, while still keeping only one copy of the shared files across them.
Whoa! That's a lot of steps to create all the subdomains I want. Is there an easier way? Well there is an alternative way. Let's get started first then we'll run the comparisons.
Add the following entry into your DNS server and change the domain and IP address accordingly.
*.example.com IN A 220.127.116.11
We are assuming that you already have a web site created for your main site: www.example.com. So let's just double check to make sure it will be able to accept all variations of the subdomains.
Add the following code to your httpd.ini in the web root. Make sure they are in the correct order.
# Convert http://example.com to http://www.example.com/
RewriteCond Host: ^example.com
RewriteRule (.*) http\://www\.example.com$1 [I,RP]
# Assuming we have limited number of shared folders.
# We will execute them accordingly regardless of the subdomain.
# Example: http://sub1.example.com/img/logo.jpg -> /img/logo.jpg
# Example: http://www.example.com/img/logo.jpg -> /img/logo.jpg
RewriteRule (/css/.*) $1 [I,O,L]
RewriteRule (/js/.*) $1 [I,O,L]
RewriteRule (/img/.*) $1 [I,O,L]
#Redirect all other subdirectories not matching
#to the list above as subdomains
#example: www.example.com\sub1 -> sub1.example.com
RewriteCond Host: www\.example\.com
RewriteRule /(\w*)/(.*) http\://$1\.example\.com$2 [I,RP]
# If the web site starts with www then point the file to the root folder
# If you specifically created a folder /www/ then you can comment out this section.
RewriteCond Host: (?:www\.)example.com
RewriteRule (.*) $1 [I,O,L]
# Any web site starts other than www will be re-mapped to /<subdomain>/
# Example: http://sub1.example.com/default.asp -> /sub1/default.asp
# Note: if the folder does not exists, then the user will get a 404 error automatically.
RewriteCond Host: (.*)\.example.com
RewriteRule (.*) /$1$2 [I,O,L]
#Fix missing slash char on folders
#This has to be at the end because if invalid dir exists,
#we should show 404 first
RewriteCond Host: (.*)
RewriteRule ([^.?]+[^.?/]) http\://$1$2/ [I,RP]
Assuming we have the company website and two subdomains created: sub1, sub2.
|URI||Location on Server|
|http://abc.example.com||/abc/ -> 404 Not Found|
The second solution is easier to implement and only requires one instance of the website, the user has to be careful about creating the folders. For example, the user can not create a folder d:\inetpub\wwwroot\example.com\sub1\img\ because it would conflict with the ISAPI_Rewrite Rule of (/img/.*). Therefore the files in that folder will not be accessible.
While ISAPI_Rewrite solution is much easier to implement, it is recommended for smaller sites with one development team. This is because of added resource and exceptions that the team has to keep track of. For a larger site, even though the admin has to setup multiple instances of the servers, these only need to be done once. Plus, the subdomains are probably developed by different teams and are distinct enough to warrant their own web instance.
So which one should you choose? Give the ISAPI_Rewrite a try first. When you think your sites are big enough you can always switch to the other method anytime.
Join us in further discussion of this topic at WebmasterWorld...
How to Create Dynamic Sub-domains in Windows IIS
Tutorial for creating sub-domains using IIS and ISAPI_Rewrite