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?