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:

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['global_vars']) .. 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?

8 Comments
  1. 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';
            }
        }

  2. 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

  3. 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_direction[1])) { $thePage = $page_direction[1]; } 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;
    

    }

    }

Leave a Reply