Wondering how blogger website gives you a subdomain from the blogspot.com domain based on your username of your choice? Well, the concept for this tutorial is somewhat related with it on setting up unlimited subdomains. I’m not sure how do they do it because it could be done by any script, hardcoded or not, thru static or dynamic way via database, many possibilities, and one possible way is thru URL rewriting via Apache’s mod_rewrite.

Requirements of this setup:

a. Apache 2.x or later
mod_rewrite module should be enable. To check if it’s enabled you should see this line on your httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so

b. BIND 9.x or later
-Setting up the DNS Server is not part of this tutorial, it will be discussed on another topic.

Anyway to create automatic subdomains, these are the steps.

1. Configure your DNS server to use the wildcard (*) and points to your IP address.
Assuming you already setup the zone of your domain on named.conf , something like:


zone “rmohan.com” {
type master;
file “/var/named/rmohan.com.db”;


$TTL 86400
@ IN SOA rmohan.com. root.rmohan.com. (
; dmn [flt.com] timestamp entry BEGIN.
; dmn [flt.com] timestamp entry END.
1D )
IN NS ns.rmohan.com.
IN NS ns2.rmohan.com.

rmohan.com. A
ns IN A
ns2 IN A
* IN A
; sub [{SUB_NAME}] entry BEGIN.
; sub [{SUB_NAME}] entry END.

As you can see above, is pointed to *. Meaning if you ping any subdomains (existing or non-existing) of rmohan.com, it will all points to the same IP address

2. Configure your Apache
-To not really messed up your existing httpd config, is it better to create a new config and include it. Something like


Include conf.d/flt/*.conf

Create your virtualhost for your domain and add this rewrite rules on it.

#vi /etc/httpd/conf.d/flt/flt.conf

DocumentRoot “/var/www/html/flt/subdomains”
ServerName rmohan.com
ServerAlias *.rmohan.com
<Directory /var/www/html/flt/subdomains>
Options Includes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all

#Rewrite Starts*
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www|m|secure|admin).* [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.freelinuxtutorials\.com
RewriteCond /var/www/html/flt/subdomains/%1 -d
RewriteRule ^(.*) /%1/$1 [L]


*Explanation (I’ll try to explain it as easy as I can)

1.RewriteEngine on = tells to enable Rewrite, off to disable it
2. It means only matches URLs that don’t start with www,m,secure or admin subdomains. This is important so it will not redirect to a particular directory e.g. m.rmohan.com to /var/www/html/flt/subdomains/m/
3. ^([^\.]+)\. ==> these are combination of regular expression syntax of mod_rewrite, meaning
^ – start of syntax
. any single character
(…) group section
+ is added to make sure there is at least one char that matches
To sum up, it denotes anything that does not have a period, example is “subdomain”
4.the -d is added to checked if that directory does not actually exist
5. ^(.*) /%1/$1 ==> it will take the requested path and put into a backreference, rewrite it to point to that directory

%1 = subdomain
$1 = path backreference, based on the Rewrite condition above
L = Last Rule

3. Restart or Reload Apache service (depends on your distro)
something like
#service httpd restart
or #/etc/init.d/apache2 restart

4. Testing
Create a subdirectory named “darwin” under /var/www/html/flt/subdomains

#mkdir -p /var/www/html/flt/subdomains/darwin
#cd /var/www/html/flt/subdomains/darwin

Create a sample php file
#vi index.php


Save and Exit

Access on your browser http://darwin.rmohan.com, this should appear:


That’s it. The subfolder name created on that directory will be your subdomain and will point it to that directory path.

Leave a comment

Your email address will not be published. Required fields are marked *

Blue Captcha Image


Protected by WP Anti Spam

Hit Counter provided by dental implants orange county