Yii2 - subdomain routing

不打扰是莪最后的温柔 提交于 2020-01-30 05:17:06

问题


I want to use subdomain as id, and I need dynamic router to do this. In urlManager, I added this line:

"http://<user:\w+>.local.dev/<controller:\w+>/<action>" => '<controller>/<action>',

When I try any action, for example:

function actionMyAccount($user){
echo $user;...
}

I am not getting anything - the var isn't printed, and script stops working (screen is white). When I remove $user, the page is loading without any problems

How can I achieve subdomain router?


回答1:


I think your router mapping setting is OK. If you want it to be more precise:

"http://<user:[^www]\w+>.local.dev/<controller:\w+>/<action:\w+>" => '<controller>/<action>'

But to make it work, you'd better double check following two things:

First, your virtual host should have a *.local.dev

  • server_name in nginx
  • or ServerAlias in Apache

Then you can use dynamic controller's name as subdomain.

Second, your virtual host should have been configured rewrite rules correctly, refer to Yii2 doc.

e.g. for Apache, just create a .htaccess file under YOUR_APP/web/ folder with following content lines:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php


来源:https://stackoverflow.com/questions/35856741/yii2-subdomain-routing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!