动态创建子域

发布于 2024-09-12 11:43:45 字数 654 浏览 4 评论 0原文

我正在使用 MVC 框架 (Yii) 创建一个网站,我需要动态创建子域,即 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/user/register。对于我的所有控制器来说,这种行为都是相同的。

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

任何建议将不胜感激!

谢谢你!

I'm creating a website using MVC framework (Yii) and I need to dynamically create subdomains, i.e. http://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.

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.

Any suggestions are greatly appreciated!

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無處可尋 2024-09-19 11:43:45

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

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

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

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

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',
)

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.

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.

小女人ら 2024-09-19 11:43:45

如果我理解你的问题,URL 的“公司”部分是一个可变的公司名称。我将在这个假设下继续我的回答。

另一种选择是创建一个公司模块(我现在将其称为“公司”),并使用 CUrlManager 规则路由到该控制器。例如,

array(
    'http://<company:\w+>.website.com/user/register' => '/companies/user/register',
    'http://<company:\w+>.website.com/<_c:\w+>/<_a:\w+>' => '/companies/<_c>/<_a>' // more generic option
),

“company”字符串将作为 $_GET['company'] 传递到应用程序,您可以在 CompaniesModule.php 文件中使用此参数来加载一些公司特定的数据。

请注意,如果没有其他规则来处理 www.website.com 请求(根据 Gray Teardrop 的回答),您将在对该子域的请求中收到错误。

If I understand your question, the 'company' component of the URL is a variable company name. I'll continue my answer under that assumption.

Another option would be to create a company module (I'll call it 'Companies' for now), and use the CUrlManager rules to route to that controller. E.g.

array(
    'http://<company:\w+>.website.com/user/register' => '/companies/user/register',
    'http://<company:\w+>.website.com/<_c:\w+>/<_a:\w+>' => '/companies/<_c>/<_a>' // more generic option
),

The 'company' string will be passed to the application as $_GET['company'] and you can use this parameter in your CompaniesModule.php file to load some company specific data.

Please note that without some other rule to handle www.website.com requests (as per Grey Teardrop's answer) you will get errors on requests to that subdomain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文