【发布时间】:2017-01-15 08:22:44
【问题描述】:
我有一个用 ASP.NET MVC 5 编写的 Web 应用程序,其中包含有关其用户的信息。现在我想为每个可作为我网站子域访问的用户创建一个个人资料页面(例如user1.example.com、user2.example.com、...)。
我希望这些子域可以通过 HTTP 访问,而应用程序的其余部分必须要求使用 HTTPS。
我在 IIS 上运行 Web 应用程序,所以我认为最好使用 URL 重写规则将 user1.example.com 重写为 example.com/Profile/Index?name=user1,这样 Web 应用程序本身就不必知道正在使用的子域。我想出了这些重写规则:
<rewrite>
<rules>
<clear />
<rule name="Rewrite user subdomains into query string" stopProcessing="true">
<match url="^(.+)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com$" />
<add input="{HTTP_HOST}" pattern="^([^.]+)\.example\.com$" />
</conditions>
<action type="Rewrite" url="/Profile/Index?name={C:1}" />
</rule>
<rule name="Redirect to https without www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
不幸的是,这段代码没有将user1.example.com 重写为example.com/Profile/Index?name=user1,甚至在user1.example.com 上强制使用HTTPS。
有人可以向我解释一下为什么以及如何解决这个问题吗?
【问题讨论】:
标签: asp.net-mvc iis multi-tenant url-rewrite-module