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.
http://delicious.com/prithish/subdomain
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:
http://www.webmasterworld.com/forum88/8746.htm
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
/home/admin/abc.com/htdocs/
then your catch-all setup shall send all sub-domain requests to that same directory
/home/admin/abc.com/htdocs/
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";
@header("Location:$loc");
}
$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";
@header("Location:$loc");
}
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.
http://mathew.pritlog.com/
http://tania.pritlog.com/
http://blakley.pritlog.com/
If anything on this post is not clear, please feel free to post a comment requesting clarification.
Tags: web - Visits: 1191 - Comments: 3
Comments:
Comment by univers11 on 2009-09-17 06:18:00Great blog post, but I'm stuck at step 1. I use directadmin as CP, and it don't allow me to create a subdomain called "*". Have you any idea how I can set this in httpd.conf?
Comment by ddfd on 2009-09-29 04:13:33
dfsd
Comment by abhijit das on 2010-01-15 05:13:27
How could we create the following sub domain or link :
http://
or
http://affiliate.vendor.hop.
Please check the site :
thanks.
abhijit