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 :

http://yourdomainname.com?page=android   
http://yourdomainname.com?page=iphone
, to be rewritten in this form:
http://android.yourdomainname.com
http://iphone.yourdomainname.com
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 *.yourdomainname.com will match for  keyword1. yourdomainname.com , keyword2. yourdomainname.com and so on.
You will need to edit the DNS zone records for your domain and add an A record for *.yourdomainname.com. , 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 www.yourdomainname.com yourdomainname.com *. yourdomainname.com
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} !www.yourdomainname.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+). yourdomainname.com [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 (http://anything.yourdomainname.com).
The actual rule is the third line, and will translate any URL that matches the above conditions to a form: http://yourdomainname.com?page=%2.





Comments

  1. Hello,
    nice blog,
    The programs and software used such as PHP and MySQL are no average persons' languages and with the appreciation that virtually everybody wants and needs a website, the push and drive for easy web hosting results is very much called for.

    ReplyDelete
  2. plz tel me clearly for creating subdomain in each user
    i created *.yourdomainname.com and add to httpd.config is over.
    i logged the form and they created subdomain.
    how to make login form in php

    ReplyDelete
  3. i logged the form and they cannot created subdomain.
    how to make login form in php

    ReplyDelete
  4. can you follow Step 3. Add rewrite rules to your .htaccess file?

    and i give you one link for login form , check it.

    http://www.phpeasystep.com/phptu/6.html

    ReplyDelete
  5. Really very useful article. keep it up.

    Thanks

    Mahesh Ruparel

    ReplyDelete
  6. I am new to technology and looking for this type of knowledgeable information, In this post you have mentioned each step very clearly. I learn a lot from this post and looking forward to your upcoming articles.Website Domain Name

    ReplyDelete
  7. I am new to technology and looking for this type of knowledgeable information, In this post you have mentioned each step very clearly. I learn a lot from this post and looking forward to your upcoming articles.
    Register a Domain

    Website Domain Name

    ReplyDelete
  8. Excellent post... Thanks for sharing... Great conversation below... I can really learn a lot.Please keep sharing more and more information.
    How To Rgister a Domain

    ReplyDelete
  9. Very nice post..this is what i was looking...Thanks for sharing
    Php Web Developer Mumbai

    ReplyDelete
  10. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

    ReplyDelete
  11. A work mate recommended me to this site. Thank you for the resources.
    Feel free to surf my web page Photo Booth Hire Adelaide

    ReplyDelete
  12. Thanks for sharing this... we can create using php www.justiworld.com

    ReplyDelete
  13. its a good one but not a complete i have setup the domain and .htaccess file but my question is that how i create subdomain by code in php after setting these all Thanks M Arfan

    ReplyDelete
  14. Thanks...
    but, how about
    http://domain.com/?sub=cool&page=10
    http://cool.domain.com/?page=10
    and
    http://cool.domain.com/10-This_my_Page.html

    ReplyDelete

Post a Comment