US: (831) 440-7361
India: +919830271197
We Create Websites We Market Websites

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.

Saturday, July 29th, 2006 PHP

7 Comments to Dynamic sub-domains with PHP

  • AjiNIMC says:

    You will have to do a * entry at your dns level as well to resolve these subdomains. It will be quite fast as it will resolve at your end.

  • John says:

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

  • AjiNIMC says:

    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.

  • Sandeep says:

    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

  • AjiNIMC says:

    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

  • Parartar says:

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

  • Yes ofcourse, it is an easy thing to do.

  • Leave a Reply

    Get a Custom Quote & Free Website Review (Report includes 200+ keywords, ranking & more - worth $200)