【问题标题】:IIS URL Rewrite Rules for Subdomain Routing in ASP.NET MVC 5ASP.NET MVC 5 中子域路由的 IIS URL 重写规则
【发布时间】:2017-01-15 08:22:44
【问题描述】:

我有一个用 ASP.NET MVC 5 编写的 Web 应用程序,其中包含有关其用户的信息。现在我想为每个可作为我网站子域访问的用户创建一个个人资料页面(例如user1.example.comuser2.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


    【解决方案1】:

    我最终使用了这段代码:

    <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>
    

    让我感到困惑的是,索引模板中的 Url 助手开始抛出 System.Web.HttpException: Cannot use a leading .. to exit above the top directory. 因为这是我唯一需要特定租户的页面,所以我放弃了,硬编码链接,一切都开始正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 2013-02-15
      • 2013-08-14
      • 1970-01-01
      • 2014-10-27
      相关资源
      最近更新 更多