duancaishi1897 2013-09-08 11:08
浏览 14
已采纳

YII重定向子域

Background

I am creating a web application using Yii, I am separating the site and supporting files from my application using sub domains. I am mapping my subdomains like so.

'urlManager'   => array(
        'urlFormat' => 'path',
        'rules'     => array(
            /* gii for developement, remove this in production */
            'gii'                                                                   => 'gii',
            'gii/<controller:\w+>'                                                  => 'gii/<controller>',
            'gii/<controller:\w+>/<action:\w+>'                                     => 'gii/<controller>/<action>',

            /* Sub domain mapping */
            'http://<module:\w+>.<hostname:[^\/]+>/'                                         => '<module>/',
            'http://<module:\w+>.<hostname:[^\/]+>/<controller:\w+>/<id:\d+>'                => '<module>/<controller>/view',
            'http://<module:\w+>.<hostname:[^\/]+>/<controller:\w+>/<action:\w+>/<id:\d+>'   => '<module>/<controller>/<action>',
            'http://<module:\w+>.<hostname:[^\/]+>/<controller:\w+>/<action:\w+>'            => '<module>/<controller>/<action>',

            /* Website URL Mappint */
            '<controller:\w+>/<id:\d+>'                                             => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'                                => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>'                                         => '<controller>/<action>',
        ),
    ),

This is all working as expected, my problem is when I need to redirect between modules. Whenever I try and $this->forward() to another subdomain I end up in the same subdomain.

Example

When on app.examplesite.com I try and forward to www.examplesite.com/user/login but end up at app.examplesite.com/user/login

$this->forward(Yii::app()->createAbsoluteUrl('user/login'));

Question

How do I correctly redirect from app.examplesite.com to www.examplesite.com/user/login?

  • 写回答

3条回答 默认 最新

  • doukeng1922 2013-09-09 06:17
    关注

    I'm not sure if this is the most elegant solution however it seems to be working for me. I extended Controller and added an extra subdomainRedirect method that allowed me to additionally pass in the subdomain I wanted to redirect to. This would then allow me to do something like this in my controller. $this->subdomainRedirect('user/login', 'www'); or $this->subdomainRedirect('/', 'admin');

    /**
     * Redirects the browser to the specified URL using another subdomain, controller and action.
     * This is similar to redirect however allows the selection of the subdomain to use when redrecting.
     *
     * @param mixed  $url        the URL to be redirected to. If the parameter is an array, the first element must be a route to a controller action and the rest are GET parameters in name-value pairs.
     * @param string $subdomain The subdomain to include in the redirect url, defaults to www.
     * @param bool   $terminate  whether to terminate the current application after calling this method
     * @param int    $statusCode the anchor that should be appended to the redirection URL. Defaults to empty. Make sure the anchor starts with '#' if you want to specify it.
     */
    public function subdomainRedirect($url, $subdomain = 'www', $terminate = true, $statusCode = 302) {
        preg_match("/^(?<protocol>(http|https):\/\/)(((?<subdomain>[a-z]+)\.)*)((.*\.)*(?<domain>.+\.[a-z]+))$/", Yii::app()->request->hostInfo, $matches);
    
        if ($url[0] !== '/') {
            $url = '/' . $url;
        }
    
        $url = $matches['protocol'] . $subdomain . '.' . $matches['domain'] . $url;
        $this->redirect($url, $terminate, $statusCode);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址