← cd ..
· 2 min read

Basecamp Style Subdomains With ExpressionEngine

Today cityzenllc asked a question on Twitter about setting up EE w/subdomains.

The “Basecamp Style Subdomains With CodeIgniter” 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:

┌─ php
 1if($_SERVER['HTTP_HOST'] == 'login.example.com')
 2{
 3    $assign_to_config['site_url'] = 'http://login.example.com/';
 4    $req = strtolower(trim($_SERVER['REQUEST_URI'], '/'));
 5    if($req == '' || substr($req, 0, 9 ) == 'index.php')
 6    {
 7        $assign_to_config['template_group'] = 'login';
 8        $assign_to_config['template'] = 'index';
 9    }
10}

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?

── comments8
Aaron Waldon · 2010-12-20

I was just looking into this functionality last week for an upcoming project. That is a very simple solution. Very nice writeup Bjørn!

Bjørn Børresen · 2010-12-22

Thanks Aaron! :-)

vinay · 2011-01-09

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'; } }

Robert · 2011-04-29

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

Robert · 2011-04-29

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

zam · 2011-04-30

hey thats a good way of doing sub domains, good codin’ :)

Robert · 2011-07-21

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 (http://twitter.com/#!/cityzenllc))) { $thePage = $page_direction1 (http://twitter.com/#!/cityzenllc); } 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; } }

Jeff Williams · 2012-05-20

Awesome, I am going to use this on my site http://weblance.com as we ar ebuilding a project management tool.

$ open [original ↗]
click or esc to close
$ open stavanger.map · last friday · børehaugen 1a · 58.974°N 5.732°E
[ .. ] fetching tiles█
drag to pan · scroll to zoom · esc to close © OpenFreeMap · OpenStreetMap