Yii Framework Forum

Dynamic subdomain creation


(Spamhouse) #1

I need to dynamically create subdomains, i.e. http://company.website.com

So, in order to achieve this I’ve added a DNS wildcard (*.website.com). Now the problem is that my application controllers are all the same for www.website.com and for company.website.com. For example, I have a User controller with Register action (user/register). Now if I go to www.website.com/user/register I can register, but I can do exactly the same thing if I go to company.website.com/user/register. And this behaviour is the same for all my controllers.

I realize everything is working correctly, but how do I separate controllers (or something like that) for www.website.com and for company.website.com? I don’t want users to access register/login/other_controllers_and_actions from the subdomian url.

Any suggestions are greatly appreciated!

Thank you!


(Ujovlado) #2

you need to create sub domains at your web hosting.

  1. delete *.website.com

  2. at your hosting in directory sub (or any other which is used for subdomains) create directory "company"

example:

/var/www/website.com/web/ … is www.website.com

/var/www/website.com/sub/company … is company.website.com

you need to create two separate applications.

:wink:


(Ujovlado) #3

or you can redirect everything to index.php and specify rules by parsing url.


(Y!!) #4

You can put full host info into the url rules:




'http://company.example.com/test' => 'site/test',



This works good, but is not very dynamic. You may create a special method/component if you need more advanced url handling. Check link in my signature about i18n. So you can get an idea of how you can archive it.


(Spamhouse) #5

I need subdomains to be dynamically created for every registered company (there will be more than 1000 companies) and this has to be one application (pretty much like tumblr.com works - subdomains for every user). So, there is no way I can create virtual hosts for each of them :)

@Y!!, thanks for your suggestion, that’s exactly how I’m tring to do (Parameterizing Hostnames). Everything works fine in my application, besides that some controllers+actions have to be accessed only from a subdomain (i.e. company.website.com/clients - ClientsController) and others have to be accessed only from the main website (i.e. www.website.com/user/register).


(Spamhouse) #6

Ok, I’ve spent some time and came up with a very ugly (at least it seems to me so) solution.

  1. I’ve created a new CApplicationComponent class (UrlAccess)

  2. Added to my config file:


'preload'=>array('log', 'urlAccess')

and in the "components" section:




    'urlAccess'=>array(

        'class'=>'UrlAccess',

    )



Now, when the application loads it first checks my new class (UrlAccess). And this what I have in the UrlAccess:




    class UrlAccess extends CApplicationComponent {


        public function init() {


            // controllers allowed to be accessed from subdomains

            $subdomainAllowed = array('manage', 'clients', 'etc.');


            // controllers allowed to be accessed from the main domain

            $domainAllowed = array('user', 'site', 'etc.');


            // check if the path has "controller/action" pattern and assign the first element to $controller

            // i.e. company.website.com/clients/new

            if(strpos(request()->pathInfo, '/')) {

                $pathInfo = explode('/', request()->pathInfo);

                $controller = $pathInfo[0];

            }

            // if the path has only controller and no action specified (i.e. company.website.com/clients)

            else {

                $controller = request()->pathInfo;

            }

            

            $urlExplode = explode('.', $_SERVER['HTTP_HOST']);


            // check if this is a subdomain

            if(count($urlExplode) > 2 && $urlExplode[0] !== 'www') {

                // check if the controller specified in the path is allowed to be accessed from subdomain

                if(in_array(strtolower($controller), $subdomainAllowed)) {

                    return true;

                }

                else {

                    throw new CHttpException('404', 'Page not found');

                }

            }

           

            // etc.

            

        }

        

        // etc.

    }



This is obviously not the whole thing, just to give you a basic idea how it works now. Now if the user goes to:

  1. company.website.com/user/login - he gets 404 error

  2. www.website.com/user/login - he gets login form

  3. company.website.com/manage (or manage/clients) - controller/action fires as it supposed to.

So, everything now works as expected. The only problem is I don’t like this solution. Is there any better approach (“Yii way”)? :)

I’m just learning, so any hints and suggestions are greatly appreciated!

Thank you.


(Spamhouse) #7

Ok, seems like nobody has a better solution? =)


(Bpnet Blog) #8

Better and truly dynamic solution for subdomains would be configuring BIND to resolve wildcard DNS.

More on this topic can be found here: debian-administration.org/articles/358

  • sorry for passive hyperlink (forum policy :/)

I presume you are using LAMP, otherwise I dunno :)


(Ashutosh2801) #9

I have url like example.com/site/blog and i want blog.example.com.

So what should i do here… Please suggest me any…

Thanks