Jul 5, 2012

f Comment

Configure Multiple Subdomains EASILY!

Amazon I own chtoen.com domain and I would like configure my DNS and web server so that I can have multiple subdomains, each pointing to any document root I like. I know one way is to define all these subdomains in DNS configuration, and then define how my web server handles each of these subdomains, but this is too time consuming. I'd like an easier way!

How do you EASILY make multiple subdomains work? What configurations do you need in DNS manager and web server in order to enable multiple subdomains? Let's set up multiple subdomains for your domain in SECONDS!

Step 1: Set DNS Correctly
You need to tell your domain's DNS server that you would like all subdomains of your domain to point to your web server's IP address. For example let's say your web server's IP is 124.3.19.87. Then you should create an 'A' record with subdomain set to '*' less quotes and IP set to 124.3.19.87, as follows:

Subdomain or Host: *
IP: 124.3.19.87
Record Type: A

Depending on your domain host the way to do this may be different, but every DNS manager should allow you to create an 'A' record with some subdomain point to some IP address. For example in Go Daddy DNS manager you should create an A (Host) record with the following values:

Host: *
Points to: 124.3.19.87
TTL: 1 Hour

This setting will make any subdomain of the domain chtoen.com point to 124.3.19.87.

However where would chtoen.com point to? chtoen.com has no subdomain and will NOT point to 124.3.19.87 UNLESS you explicitly create an A record for it. In GoDaddy DNS manager, for example, you should create an A (Host) record with the following values:

Host: @
Points to: 124.3.19.87
TTL: 1 Hour

In GoDaddy DNS manager entering "@" for the host name is the same as entering your domain name, minus the "www". In another domain registration platform's DNS manager you may need to enter something else to mean the same thing, or just leave the text field empty. It totally depends on the domain registration system.

I know it's stupid that you need to create two A records in order to point chtoen.com and any subdomain of chtoen.com to 124.3.19.87, but that's just the way the cookie crumbles!

Let me know if you don't know how to do it!

Step 2: Configure Web Server Correctly
Now we simply tell our web server how to handle traffic that comes from each subdomain that we want to handle. The configurations depend on the web server you use. I use Apache2, and I have the following in my Apache2 configuration file which is /etc/apache2/site-available/all:

<virtualhost *>
ServerName demo.chtoen.com
DocumentRoot /home/ubuntu/repository/demo
</VirtualHost>

<virtualhost *>
ServerName www.chtoen.com
DocumentRoot /home/ubuntu/repository/chtoen
</VirtualHost>

#BLOCK
<virtualhost *>
ServerAlias *.chtoen.com
DocumentRoot /home/ubuntu/repository/trunk
</VirtualHost>
#end BLOCK

<virtualhost *>
ServerName chtoen.com
RewriteEngine on
RewriteRule ^/(.*) http://www.chtoen.com/$1 [L,R=301]
</VirtualHost>

This set of configurations should be very self explanatory. Requests that go to subdomain demo.chtoen.com would access /home/ubuntu/repository/demo directory. Requests that go to subdomain www.chtoen.com would access /home/ubuntu/repository/chtoen directory. Requests that go to any other subdomains (e.g. abc.chtoen.com) would go to /home/ubuntu/repository/trunk directory. Isn't it simple?

Suppose you don't have the configuration marked as BLOCK above then abc.chtoen.com might go to the first VirtualHost definition in your configuration file you have but it's really up to the web server how they want to handle this situation. This is my Apache web server's behavior. My Apache's version is 2.2.14 as shown below:

ubuntu@ip-10-16-4-7:~/repository$ apache2 -v
Server version: Apache/2.2.14 (Ubuntu)
Server built: Mar 5 2012 16:41:39

The last definition basically tells Apache to permanently redirect chtoen.com to www.chtoen.com. For example http://chtoen.com/2010/03/how-to-remove-virus-from-computer.html would 301 redirect to http://www.chtoen.com/2010/03/how-to-remove-virus-from-computer.html.

Any questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael