How to create a subdomain in CakePHP?

CakePHP Version 2.5.5 Version

My domain name is http://www.thechatfun.com

Link to profile page - http://www.thechatfun.com/users/profile

Link to the chat page - http://www.thechatfun.com/chats/index

Over the two links I want to look like http://profile.thechatfun.comandhttp://www.chat.thechatfun.com

I cannot make a subdomain in CakePHP.

Please help me

Thanks ChatFun

+4
source share
3 answers

While you can set up your entries in the domain to indicate both subdomains in both chat and your profile, you can change the htaccess file in the web root folder and add.

    <IfModule mod_rewrite.c>
      #standard cake htaccess stuff
      ...

      RewriteCond %{HTTP_HOST} ^profile\.thechatfun\.com$ [NC]
      RewriteRule ^(.*)$ http://www.thechatfun.com/users/profile/$1 [R=301,L]

      RewriteCond %{HTTP_HOST} ^chat\.thechatfun\.com$ [NC]
      RewriteRule ^(.*)$ http://www.thechatfun.com/chats/index/$1 [R=301,L]

    </IfModule>

I have this exact requirement and it works for me.

+4
source

: /lib/Cake/Routing/Route, SubdomainRoute.php :

class SubdomainRoute extends CakeRoute {

    public function match($params) {
        $subdomain = isset($params['subdomain']) ? $params['subdomain'] : null;
        unset($params['subdomain']);
        $path = parent::match($params);
        if ($subdomain) {
            $path = 'http://' . $subdomain . '.thechatfun.com' . $path;
        }
        return $path;
    }
}



, .

echo $this->Html->link(
    'Profile',
     array('subdomain' => 'profile', 'controller' => 'Users', 'action' => 'profile')
);

echo $this->Html->link(
    'Chats',
     array('subdomain' => 'chat', 'controller' => 'Chats', 'action' => 'index')
);


: http://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-0.html#routes-can-return-full-urls

+1

profile.thechatfun.com www.chat.thechatfun.com - . , http , .

, - - Apache, - . VirtualHost, , .

Your web server first receives an HTTP request, then passes the request to PHP for processing. So, if your web server is not configured properly to handle these domains, you will not be able to control this in PHP or CakePHP.

+1
source

All Articles