Posted By

brettbergeron on 08/20/09


Tagged

php subdomain codeigniter


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

omardixon
vali29
umang_nine
dantreacy
jatap
infinidad


Call CodeIgniter Application Based on Subdomain


 / Published in: PHP
 

URL: http://brettbergeron.com

I recently found the need to run multiple CodeIgniter applications inside of a single codebase for development & test purposes. So, I rewrote a few lines of the primary index file (/index.php) to automatically route URLs like subdomain.domain.com to the CodeIgniter application within /system/application/subdomain.

As a catch-all, the snippet will route the "www" subdomain (and requests with no subdomain) to application/live.

This is a nice & tidy way to give your development, test and live versions of a site their own unique subdomains. Works great when passing the URLs to clients for testing & approval.

  1. $domain = $_SERVER['HTTP_HOST'];
  2.  
  3. if(fnmatch('*.*.*', $domain)) {
  4. $subdomain = explode(".", $_SERVER['HTTP_HOST']);
  5. $application_folder = ($subdomain[0] == "www") ? "application/live" : "application/".$subdomain[0];
  6. } else {
  7. $application_folder = "application/live";
  8. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: brettbergeron on August 20, 2009

Oops! I forgot to mention that the pattern matching for the subdomain will only work for top-level-domains that only have one component. Thus, URLs with the .co.uk format will have to adjust the arguments in fnmatch() on line 3.

Posted By: d3zign7reak on September 6, 2009

How would implement the above code with the following URL: http://subdomain.domain.com

You need to login to post a comment.