Subdomaining with Cake

By Mumtaz aka "Mum_Chamber"
In this article, following a practical approach to subdomaining, we will make one model available through subdomains. Can be inspiring for alternative uses.
Inspired by the Hosting Admin URLs on a Subdomain at http://bakery.cakephp.org/articles/view/hosting-admin-urls-on-a-subdomain, I have tried to come up with some piece of code that makes uses of subdomains in an elegant way.

The aim is to access all instances of a model through subdomains. For instance, every user may have their subdomains, accessible at username.example.com

In order to deal with this, I assume that you already made the necessary implementations in your controller. Some sample code would look like (user_controller.php) :
Download code function view($unique_title){
    $this->set('users', $this->User->findByUniqueTitle($unique_title) ); 
}


Also, you need to route your users model at app/config/routes.php with something like:

Download code Router::connect('/user/*', array('controller' => 'user', 'action' => 'view'));

Now, what we will actually do is run some piece of code before the MVC implementation is started. The correct place to do this is the app/config/bootsrap.php

bootstrap.php :
Download code $subdomain = substr( env("HTTP_HOST"), 0, strpos(env("HTTP_HOST"), ".") );
if( strlen($subdomain)>0 && $subdomain != "www" ) {
    $_GET["url"] = "user/" . $subdomain . "/" . (isset($_GET["url"]) ? $_GET["url"] : "");
}


This piece of code is pretty self explanatory. if the subdomain is different than "www", the page displayed is users/<subdomain>/<url>. So, mumchamber.example.com/about is actually displaying the url www.example.com/users/mumchamber/about

To test subdomains on localhost, you may want to have a look at http://digitalpbk.blogspot.com/2007/01/making-subdomains-on-localhost.html Only remember that in linux, your hosts file is probably located at /etc/hosts

Also, you may want to see http://httpd.apache.org/docs/1.3/vhosts/ for virtual host documentation

Comments 614

CakePHP team comments Author comments

Comment

1 maybe htaccess

Why don`t you use .htaccess?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?(([a-z0-9_-]{1,100}).)?example.com$
RewriteRule ^(.*)$ /users/%3/about [L]

Regards
posted Tue, Feb 5th 2008, 04:25 by MichaƂ Bachowski

Comment

2 Why not htaccess

Well, even though .htaccess is also a nice way of dealing with subdomains, my personal experience shows that it's not the best solution for this particular case. Usually, if you make a model accessible through subdomains, you want to have exceptions. For instance, you may want to perserve admin.example.com, docs.example.com, wiki.example.com for obvious purposes. In this case, .htaccess gives you less (not little, only less) flexibility. With this piece of code, you can very easily implement restrictions or apply changes.
posted Tue, Feb 5th 2008, 10:39 by Mumtaz

Comment

3 Thanks

This was very helpful - thanks! Though I did find people kept trying to access my site via http://www.username.example.com so I did:


    $url_parts = explode('.', env('HTTP_HOST'));
    foreach($url_parts as $part) {
        if(!in_array($part, array('www', 'example', 'com'))) {
            $ident = $part;

        }
    }
posted Tue, Feb 19th 2008, 18:09 by Brit Gardner

Comment

4 Nice idea for social sites

thanks for your code.
i think it will help me a lot on my 'blogging' project.
currently, i am working on a 'social bookmarking' project. will try to implement your code so that users bookmarks can be accessed from user.sitename.com ;)
posted Fri, Feb 29th 2008, 01:27 by Fahad Ibnay Heylaal

Comment

5 nice article

i have read another article from http://www.ipuh.org/items/view/48:how-to-add-subdomain-on-cakephp
i have problem too with subdomain in cakephp.This article helpful.
posted Thu, Mar 20th 2008, 16:11 by budi allay

Login to Submit a Comment