动态创建子域 [英] Dynamic creation of subdomains

查看:30
本文介绍了动态创建子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MVC 框架 (Yii) 创建一个网站,我需要动态创建子域,即 http://company.website.com

I'm creating a website using MVC framework (Yii) and I need to dynamically create subdomains, i.e. http://company.website.com

因此,为了实现这一点,我添加了一个 DNS 通配符 (*.website.com).现在的问题是我的应用程序控制器对于 www.website.com 和 company.website.com 都是一样的.例如,我有一个带有 Register 操作(user/register)的 User 控制器.现在,如果我去www.website.com/user/register,我可以注册,但如果我去company.website,我可以完全做同样的事情.com/用户/注册.这种行为对我所有的控制器都是一样的.

So, in order to achieve this I've added a DNS wildcard (*.website.com). Now the problem is that my application controllers are all the same for www.website.com and for company.website.com. For example, I have a User controller with Register action (user/register). Now if I go to www.website.com/user/register I can register, but I can do exactly the same if I go to company.website.com/user/register. And this behaviour is the same for all my controllers.

我意识到一切正常,但是如何将 www.website.com 和 compnay.website.com 的控制器分开?我不希望用户从 subdomian url 访问注册/登录/其他控制器和操作.

I realize everything is working correctly, but how do I separate controllers for www.website.com and for compnay.website.com? I don't want users to access register/login/other controllers and actions from the subdomian url.

非常感谢任何建议!

谢谢!

推荐答案

您可以将主机名包含到路由规则数组中.例如,您可以创建规则

You could include hostname into your routing rules array. For example, you could create rules

array(
    'http://www.website.com/user/register' => 'user/register',
    'http://<company:\w+>.website.com/user/register' => 'other/route',
)

并检查 other/route 操作中的 company 参数.请注意,这些规则需要 http:// 才能起作用.有关详细信息,请参阅 CUrlManager 文档.

and check for company parameter in your other/route action. Please note that http:// is required for those rules to work. See CUrlManager documentation for more details.

附言如果 http://www.website.comhttp://company.website.com/user/register 的控制器完全不同,最好设置这些网站的两个应用程序.

P.S. If controllers for http://www.website.com and http://company.website.com/user/register are completely different it could be better to set up two applications for those sites.

这篇关于动态创建子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆