Levi Putna
Levi Putna

Reputation: 3073

YII redirect with subdomain

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?

Upvotes: 0

Views: 3178

Answers (3)

biboMandroid
biboMandroid

Reputation: 117

 public function subRedirect($url, $subdomain = 'www', $terminate = true, $statusCode = 302)
{
    preg_match("/^(?<protocol>(http|https):\/\/)(((?<subdomain>[a-z]+)\.)*)((.*\.)*(?<domain>.+\.[a-z]+))$/", Yii::app()->request->hostInfo, $matches);
    // $url is in format array(controlerID/actionID) 
    $params = $url;
    unset($params[0]);
    $final = $this->createUrl($url[0], $params);
    $url = $matches['protocol'] . $subdomain . '.' . $matches['domain'] . $final;
    $this->redirect($url, $terminate, $statusCode);
}

Upvotes: 1

Levi Putna
Levi Putna

Reputation: 3073

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);
}

Upvotes: 1

Zack Newsham
Zack Newsham

Reputation: 2992

I would think that if you enter a record something like this:

'www.examplesite.com/user/login'=>'/user/login'

you could then call this from anywhere

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

Upvotes: 0

Related Questions