Username as Subdomain in Codeigniter a.k.a Wildcard Subdomain

I’ve been wondering for a while if I can do automatic subdomain using username, same as Wordpress MU does. Some thought come across and all leads to same point , the $base_url() variable.

What makes me a bit wary is that particular variable reside in config file, which suppose to be static. On the other hand, manipulating the variable in controller is easy but redundant. So what the heck…
There are three places to modify for this :

  1. DNS entry
  2. Apache vhost
  3. Codeigniter config.php

Life has been good to me since I have full control over those three, I have my own DNS server, and apache server, so if you are less fortunate than I am, do not hesitate to nag , beg and annoy your hosting company to do changes for you

DNS entry :

make sure you put wildcard entry :
mygreatsite.com. A 123.123.123.001
*.mygreatsite.com. A 123.123.123.001

Apache vhost :

We use separate vhost file for each domain, and include those files using Apache “Include” directive, to keep things tidy and avoid fiddling with main apache config.
<virtualhost>
ServerName mygreatsite.com
ServerAlias *.mygreatsite.com
ServerAdmin superduperadmin@superduperwebcompany.com
#RedirectMatch 301 (.*) http://mygreatsite.com$1
DocumentRoot /var/oi/keepcodeigniterhere
<directory>
Options Indexes FollowSymLinks MultiViews
</directory>
AllowOverride All
Order allow,deny
allow from all
</virtualhost>

the most important part is :
#RedirectMatch 301 (.*) http://mygreatsite.com$1
CodeIgniter config.php :

I test using this simple hack ( on the config.php ) :
$config['base_url'] = "http://".$_SERVER["HTTP_HOST"];

And wala !! I can test using whatever.mygreatsite.com and codeigniter still working as if there is nothing wrong with $base_url variable, therefore you can use any words ( hence, wildcard ) as subdomain prefix and apache will direct the request to the same codeigniter installation. Next step I will do is using username as subdomain, eliminating ugly url like
http://www.mygreatsite.com/someuser/profile
and make it nicer as in
http://someuser.mygreatsite.com/profile
Nice !!
Yeah I know I have to test rigorously to ensure nothing is broken with codeigniter ( and my application ) but at this moment I think it leads to the right direction.
I also do some search at CodeIgniter’s forum just to verify my method and I’ve found great reference :
http://codeigniter.com/forums/viewthread/51627/

For wildcard DNS, take a look at this guide by Wordpress creator :
http://photomatt.net/2003/10/10/wildcard-dns-and-sub-domains/

from those forum entry, I’ve made final modification to the config.php file :
$config['base_url'] = "http://".$_SERVER["HTTP_HOST"];
// provide easy to access variables for the rest of the application
$usernamehost = explode('.', $_SERVER['HTTP_HOST'], 2);
$config['tld_base_url'] = $usernamehost[1]; // contains "domain.com"
$config['user_base_url'] = $usernamehost[0]; // contains "sub"

Hopefully this will work as expected , and also useful to anybody who wants to achieve similar thing.


About this entry