on
Basecamp Style Subdomains With ExpressionEngine
Today cityzenllc asked [a question on Twitter][2] about setting up EE w/subdomains.
The [“Basecamp Style Subdomains With CodeIgniter”][3] over at NetTuts was tweeted and retweeted all over the place, or maybe just twice, but anyway – this is supereasy in EE, and can be done by adding just a couple of lines to index.php:
if($_SERVER['HTTP_HOST'] == 'login.example.com')
{
$assign_to_config['site_url'] = 'http://login.example.com/';
$req = strtolower(trim($_SERVER['REQUEST_URI'], '/'));
if($req == '' || substr($req, 0, 9 ) == 'index.php')
{
$assign_to_config['template_group'] = 'login';
$assign_to_config['template'] = 'index';
}
}
What the above snippet of code will do is check if the current URL the user is on is ‘login.example.com’ – if it is it will change the ‘site_url’ variable to override the default one (so that {path=”} etc will use that URL. Also it will make sure the ‘index’ template in the template group called ‘login’ is used (if the user is not on a subpage that is).
Of course you could add all kinds of magic here – having Apache accept all and every subdomain, fetching a specific template group and adding the subdomain to the global variables (_$assign_to_config[‘globalvars’]) .. for instance to implement a username.example.com solution. You could even throw some .htaccess in the mix if you really needed it ;)
How have you solved this with ExpressionEngine?
[2]: http://twitter.com/#!/cityzenllc/status/16577549404479488 “” [3]: http://net.tutsplus.com/tutorials/php/basecamp-style-subdomains-with-codeigniter/ “”
Comments
Comment by Aaron Waldon on 2010-12-20 20:23:56 +0000
I was just looking into this functionality last week for an upcoming project. That is a very simple solution. Very nice writeup Bjørn!
Comment by Bjørn Børresen on 2010-12-22 10:59:02 +0000
Thanks Aaron! :-)
Comment by vinay on 2011-01-09 16:08:52 +0000
Thanks Bjorn. I am creating a location based sub domain website in EE.
Your code really helps. I modified it to
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain_name = $subdomain_arr[0];
if(!empty($subdomain_name))
{
$assign_to_config['site_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';
$req = strtolower(trim($_SERVER['REQUEST_URI'], '/'));
if($req == '' || substr($req, 0, 9 ) == 'index.php')
{
$assign_to_config['template_group'] = 'home';
$assign_to_config['template'] = 'index';
}
}
Comment by Robert on 2011-04-29 06:34:40 +0000
I seem to be having trouble. I’ve used Vinay’s adaption, but it seems that my hosting is taking over when you enter a subdomain.
Am I missing something?
arenowengaged.com is the URL I’m working on.
Thanks, Robert
Comment by Robert on 2011-04-29 06:57:05 +0000
I should probably add some information…
Again, the URL is http://www.arenowengaged.com
This is a sub page which I’ve created to test this code: http://www.arenowengaged.com/index.php/Sub_Domains/leeandjess
Thanks
Comment by zam on 2011-04-30 09:30:08 +0000
hey thats a good way of doing sub domains, good codin’ :)
Comment by Robert on 2011-07-21 19:11:28 +0000
I ended up getting everything to work. However, I had to modify what Vinay did just a little. Here’s what I came up with:
$subdomain_arr = explode(‘.’, $_SERVER[‘HTTP_HOST’], 2); $page_direction = explode(‘/’, $_SERVER[‘HTTP_HOST’]);
if (!empty($page_direction1)) { $thePage = $page_direction1; } else { $thePage = ‘index’; }
$subdomain_name = $subdomain_arr[0]; if(($subdomain_name != ‘www’) && (!empty($subdomain_name))) {
$assign_to_config['site_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';
$req = strtolower(trim($_SERVER['REQUEST_URI'], '/'));
if($req == '' || substr($req, 0, 9 ) == 'index.php') {
$assign_to_config['template_group'] = $subdomain_name;
$assign_to_config['template'] = $thePage;
//echo $thePage;
}
}
Comment by Jeff Williams on 2012-05-20 20:52:30 +0000
Awesome, I am going to use this on my site http://weblance.com as we ar ebuilding a project management tool.