ASP.NET MVC中的子域路由的IIS URL重写规则5
问题描述:
我有一个用ASP.NET MVC 5编写的Web应用程序,其中包含有关其用户的信息。现在我想为每个可以作为我的网站的子域访问的用户创建一个配置文件页面(例如user1.example.com
,user2.example.com
,...)。ASP.NET MVC中的子域路由的IIS URL重写规则5
我希望这些子域可以通过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
改写user1.example.com
到example.com/Profile/Index?name=user1
和力量HTTPS。
有人请向我解释为什么以及如何解决这个问题?
答
我结束了使用此代码:
<rewrite>
<rules>
<clear />
<rule name="Rewrite url with tenant subdomain" stopProcessing="true">
<match url="^$" /> <!-- So resources like user1.example.com/Content/css wouldn't be affested by this rewrite -->
<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?subdomain={C:1}" />
</rule>
<rule name="Redirect to www.example.com" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$"/>
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
了什么我困惑的事实,在指数模板网址助手开始扔System.Web.HttpException: Cannot use a leading .. to exit above the top directory.
因为它是我需要有特定于租户的唯一的页面,我给硬编码的链接和一切工作正常。