• Home
  • About
  • Who Am I?
  •  

    Virtual Subdomains and PHP

    With the ever dynamic web world and to turn the web from a passive to an active entity , webmasters are fascinated with the idea of having subdomains for users like http://username.domain.com. Looks quite neat and tricky. So lets see how we can achieve this with apache and some PHP.

    First i go in my server and change the DNS zone for the requested domain , lets say abc.com. On WHM you can accomplish this easily by going into WHM , then clicking Edit DNS Zone, this will show you the list of DNS zones available on the server , from the list choose abc.com , click Edit. Now you will see various DNS entries for abc.com , in an empty box  at the bottom ,  add a wildcard (*) CNAME entry something like this..

    * 14400 IN CNAME abc.com.

    If you are not using WHM , you will have to manually find the DNS zone file for abc.com on your server and edit it to take effect like above.

    Now what we have done is , we have created a wild card subdomain , meaning that if the subdomain requested doesnt exist , this wild card subdomain will catch it , you can also call this a Catch-All subdomain.

    Next i go and edit my httpd.conf file and add a virtual host something like this…

    <VirtualHost 11.11.11.11>
    ServerAlias *.abc.com
    ServerAdmin admin@abc.com
    DocumentRoot /home/abc/public_html/subdomains
    ServerName *.abc.com
    User abc
    Group abc
    </VirtualHost>

    In above block what i have done is , i have pointed the requests for *.abc.com to a different folder than the main domain , this folder can be inside your main domain folder as well , in my case the folder is inside the main domain folder and is called “subdomains”. So any subdomain that is not actually found in the DNS entry will point to this entry in the httpd.conf file and that in turn will point to the folder /home/abc/public_html/subdomains .

    Now after doing the above you can put anything inside subdomains folder i.e example.php and you can call this via your browser like this. http://anything.abc.com/example.php , works good , eh?

    Now what i will do inside my example.php is ….

    $exp = explode(”.”, $HTTP_HOST) ;
    $username = $exp[0];

    Now what we have done above is , exploded the HTTP_HOST variable and found the username. Once we have that , we can bring in the rest of the data based on it from the DB.The best thing about this approach is that it doesnt overload the DNS server with 100s of subdomain entries.

    I find this a very easy and usefull approach , hope this helps someone else as well. Have fun!


    7 Responses to “Virtual Subdomains and PHP”

    1. Mark Garbers Says:

      Hey there,
      Thanks so much for this entry- I’ve spent hours trying to get this right and your post solved it in a few minutes!
      I’ve used catch all subdomains on several sites of mine, but this is the first time that I’ve had to set them up myself.
      Kind regards
      Mark

    2. Shouvik Says:

      Hello,

      I was looking for such a tutorial for the past 1 week and this tutorial has actually solved my problem,

      Thanx a lot for sharing this with us

      God Bless
      Shouvik

    3. Mario Says:

      I need this soooooooooooooo bad, I am gonna try it and if it works, I am buying you a drink!!

    4. Tom Says:

      Hi,

      Something happened to my virtual subdomains and they no longer work. Apache and probably the entire system and server software was upgraded. The site works, but my subdomains do not. The .htaccess file was missing after the upgrade. I copied up my backup, but any subdomain entered defaults to the Apache default page as if I just setup a new account. Again, the main page loads fine for the domain.

      Any areas to look? PHP files have not been updated since Feb, March, or April of 2008. The only thing that has changed was an Apache and CPanel upgrade. Not sure about PHP and MySQL, but mod_rewrite works fine since I created a link1/link2 simple test.

      Thanks,

      Tom

    5. Matt Hough Says:

      That is a long way round to do things. let me help you make life simpler in the future..

      1. login to Cpanel
      2. go to subdomains
      3. create a subdomain with the name * (example *.advtise.com)
      4. all done!

      and as for your php code, maybe helpfull for others to use:

      $exp = explode(”.”, $_SERVER[’HTTP_HOST’]) ;
      $username = $exp[0];

      echo “you are username: “. $username;

      Note $_SERVER

      informative post, but no need when you can just do this way.

    6. Matt Hough Says:

      oh, and I forgot, you can set up the root folder of it in the subdomain creation, where you will place your php file

    7. Fouad Says:

      Matt Hough… You Rock! Thanks!

    Leave a Reply