Problem with linking from backend to frontend when using subdomains

My frontend url is: fr.example.com

My backend url is: fr.admin.example.com

Inside my backend I want to create links to frontend, but whatever I do, it generates me link with this .admin inside of it. Here is what I am tring:

Inside my /backend/config/main.php


'frontendUrlManager' => [

    'class' => 'common\components\UrlManager',


    'languages' => ['fr', 'en', 'de'],

    'enablePrettyUrl' => true,

    'showScriptName' => false,

    'enableLanguageDetection' => false,

    'rules' => [

        'http://<site:\w+>.example.com/<controller:[\w-]+>' => '<controller>/index',

        'http://<site:\w+>.example.com/<controller:[\w-]+>/<action:[\w-]+>' => '<controller>/<action>'

    ],

],

<site:\w+> inside my rules is replaced with ‘fr, en, or de’ subdomains, and I am using https://github.com/codemix/yii2-localeurls for language localization.

I am creating links with


Yii::$app->frontendUrlManager->createAbsoluteUrl($url);

and $url passed is :


array(2) { [0]=> string(11) "comment/view" ["id"]=> int(112233) }

This code is always generating url : fr.admin.examle.com, and I was expecting .admin to not be there, since I want link to frontend.

Any clues what may be wrong, or how to do this properly ? I am using Yii 2.0.9

Normally this could be solved by using "baseUrl" or "hostInfo" properties. But in your case, since there are multiple languages, this idea may not be possible.

if these two properties are not set, application will append the url to the hostInfo of the Request which will be fr.admin.example.com in case of backend.

You may have to modify the way you are using the rules.

One workaround is to write your own class which returns the url generated (not absolute) appended to http(s):://<<language>>.example.com

You have to follow this guide:

https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/topic-link-backend-frontend.md

Basically, two UrlManagers: one for the frontend, and one for the backend.

The advanced application template provides two separate applications. That’s important.