Dynamic sub-domains with PHP

I got this email from one of the php usergroups,

“I have to create subdomains dynamically by php when an user registered in my site. Anybody who knows the script and procedure , please send me ASAP.”

I hate repeating things so better let it go live than bury inside a usergroup. Here I go with the answer.

Step 1: Make the following entry to your apache http.conf
ServerAlias *.domain.com ( do not forget to restart apache 🙂 )
Step 2: Now remember that all the query comes to same doc root. Now you need to catch it using .htaccess and process it the way you want.

Also make sure your dns is able to process your sub-domains properly. These simple steps will make it happen.

7 Replies to “Dynamic sub-domains with PHP”

  1. Could you give an example of a .htaccess file for step 2? It isn’t really clear what you are saying to do. Thanks.

  2. something like


    (This is multi blogs)
    RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]{3,}).idealwebtools..com
    RewriteCond %{REQUEST_URI} !/common-blogs/*
    RewriteCond %{REQUEST_URI} !/styles-for-common-blogs/*
    RewriteRule (.*) /commonblogs/$1 [L]

    Over here all the request from subdomain (with more than 3 letters) will get executed from commonblogs folder than public_html, this .htaccess should be placed at public_html (doc root) level.

    Hope it helps.

  3. Hello,

    I want to know if i use abc.idealwebtools.com/test.php then above .htacess code is enought or do i have to add some extra condition to handle this request.

    For abc.idealwebtools.com/test.php
    i want it should open /commonblogs/abc[subdomainname]/test.php

  4. For abc.idealwebtools.com/test.php
    i want it should open /commonblogs/abc[subdomainname]/test.php

    The above htaccess will not work as it was not getting it from /commonblogs/abc[subdomainname]/ .. for this you will have do add some more lines.
    RewriteRule (.*) /commonblogs/%1/$1 [L]

    I think this should work, in case if it is not working let me know I will write the code for you :). Good day

  5. Can you actually write the code to make this happen?
    And can you also mention where exactly the code should be placed?
    Greatly appreciated

Leave a Reply

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