Home Archive RSS Login

I learned something new today

Create subdomains in PHP

When I started receiving more and more requests for installing Pritlog on my server for different folks, I wanted to think of a better solution than just installing a separate Pritlog copy to each individual and put them in separate sub directories. This can cause any modification or updations to all these different installations to be a very time consuming task.  

This is when I started thinking about the possibility of modifying Pritlog in such a way that a single installation could handle multiple blogs and these blogs can be in separate subdomains - similar to how it works in Blogspot and few other sites.

First task was to create sub domains dynamically using PHP. Usually, sub domains can be created from the control panel provided by your hosting provider. But, this is a manual task. I did some searching through the net and came across some articles that talked about using a XML API provided by cPanel (hosting control panel) that could be used. My delicious link below has the bookmarks I made during my research.


But, none of the API solutions worked for me. I was trying this on my hosting account with Hostmonster. Finally, the below solution worked for me.
The original discussion link:

The steps that actually helped me.

Step 1: First you need to set your main domain to act as a catch-all subdomain. its like putting ServerAlias * so that each subdomain which is requested
to the main domain reaches the same place. I mean if your main domain root is


then your catch-all setup shall send all sub-domain requests to that same directory


Initially, I could not figure out how to do it in Hostmonster. Finally, I found the solution somewhere that said, we could add a subdomain called '*' and make it direct to the root of the domain. This way, my domain became a catch all domain.

Step 2: You'd add this code to very top of your index page right after ""

$domain = $_SERVER['HTTP_HOST'];
$domain_parts = explode('.',$domain);
if (count($domain_parts) == 3 && $domain_parts[0]!= "www") {
   // make sure a subdomain is called
   $user = $domain_parts[0];
   $loc = "http://domain.tld/whateverpage.ext?varname=$user";    

What this step is basically saying is when the domain is a catch all domain, all requests to any subdomains will come to the index files of the root domain. You can add this code to parse the data in the requested url and figure out what subdomain was requested and use "header" function from within your index script to handle the redirection.

I am now using this to provide hosted Pritlog. Here are some links to check this out.

If anything on this post is not clear, please feel free to post a comment requesting clarification.

Author: Prit -  Date: 26 Apr 2009 01:26
Tags: web -  Visits: 70164 -  No Comments


No comments posted yet!

Add Comment

Comment Form


 (Optional, will not be published)

 (Optional, format: http(s)://website.com)