Thursday, July 15, 2010

Create dynamic subdomain using Mod rewrite

This article is for dynamic subdomain using mod rewrite. How can you create dynamic subdomain using htaccess or Mod rewrite ? In this article you can create dynamic subdomain only three steps. Ha ha ha !
You might have noticed some sites having their URLs written as if they were different subdomains of the same domain.

For example, for a news site that has the dynamic links of the following form :
, to be rewritten in this form:
You can achieve this without actually creating the subdomains on your server. This can be done with Apache mod_rewrite.
For this method to work you need a server that runs Apache and has mod_rewrite module installed. Also, if you don't have access to server configuration files, then you might need some help from your hosting support for the second step and possibly with the fists step of this tutorial. 
Step 1.  Create a wildcard DNS A record in your DNS Zone.
A wildcard DNS record is a record represented by the character "*”, that will match all non existing sub domain names. For example * will match for  keyword1. , keyword2. and so on.
You will need to edit the DNS zone records for your domain and add an A record for * , pointing to the IP for your domain.
If your domain  is on a shared hosting account, then you will need to search in your control panel for a section for DNS management. Enter a new A type DNS record, set as name "*" and enter the IP for your domain. If you cannot find a DNS management section on your hosting account, then you will have to ask your hosting support to add it for you. 

Screen 1

Screen 2

Step 2.  Setup wildcard ServerAlias
You must now configure Apache server to serve the pages that come for any subdomain of your domain. Do this by adding a wildcard ServerAlias.
Add the following line to httpd.conf VirtualHosts section for your domain:
ServerAlias *.
Again, if you are on a shared hosting and don't have access to httpd.conf file, and no means in your control panel to edit your VirtualHosts section, then you must ask help from your hosting support.
Step 3. Add rewrite rules to your .htaccess file
First you must prepare your .htaccess file. If the file does not exist, create it. You might already have these lines in your .htaccess files, if you do don't add them again.  

Options +FollowSymLinks
You need this option enabled for your rewrite rules to work. But in most cases is already enabled in the configuration files for the http server.  
RewriteEngine On
Use this line if RewriteEngine is not already enabled on your server.
RewriteBase /
If your code is in the root of your domain, then use / as the rewrite base, like in the line above. If it is in a folder, then use the path to that folder, for example:
RewriteBase /subdomain/

Add the actual rewrite rules: 
RewriteCond %{HTTP_HOST} !$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+). [NC]
RewriteRule (.*) index.php?page=%2 [NC,QSA]
First 2 lines add the condition for the actual rewrite rule (line 3) to come in place. 
First line will add an exception for the subdomain www. Add here a similar line for each subdomain that you don't want to follow the same rule.
Second line adds the condition that the URL is in a form that looks like a subdomain, with or without the www string in front (
The actual rule is the third line, and will translate any URL that matches the above conditions to a form: