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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮