【问题标题】:How to create a sub-domain in CakePHP?如何在 CakePHP 中创建子域?
【发布时间】:2015-01-20 18:11:27
【问题描述】:

CakePHP 版本-2.5.5

我的域名是http://www.thechatfun.com

个人资料页面链接 - http://www.thechatfun.com/users/profile

聊天页面链接 - http://www.thechatfun.com/chats/index

上面两个链接我想看起来像http://profile.thechatfun.comhttp://www.chat.thechatfun.com

我无法在 CakePHP 中创建子域。

请帮帮我

谢谢 ChatFun

【问题讨论】:

标签: php .htaccess cakephp


【解决方案1】:

只要您可以将域记录配置为将聊天和配置文件子域都指向您的服务器,那么您就可以更改 webroot 文件夹中的 htaccess 文件并添加..

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

我有这个确切的要求,这对我有用。

【讨论】:

  • 我将在哪个.htaccess 文件中添加此代码?在 cakephp 中有 3 个 .htaccess。在应用程序文件夹之外。在 app 文件夹和 webroot 文件夹中。
  • @Chinu 编辑app_folder/.htaccess 文件,而不是webroot/.htaccess
  • @NguyễnAnhTuấn 感谢您在很长一段时间后发表评论。
【解决方案2】:

按照您的上下文,在此目录中:/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

【讨论】:

  • 在哪里可以调用 SubdomainRoute?
【解决方案3】:

profile.thechatfun.com 和 www.chat.thechatfun.com 是不同的域。单个 http 服务器可以同时处理这两个域,但这不会自动发生。

假设您的网络服务器是 Apache,您首先需要配置网络服务器以正确处理这些域。您可以添加一个 VirtualHost 指令,以便这两个域都由同一个虚拟主机处理并共享一个文档根目录,或者您可以为每个域添加一个虚拟主机并为每个域具有单独的文档根目录。

您的网络服务器首先接收 HTTP 请求,然后将请求传递给 PHP 进行处理。因此,如果您的网络服务器没有正确配置来处理这些域,您将无法在 PHP 或 CakePHP 中控制它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多