IIS 7上的条件https重定向

时间:2012-12-29 22:19:30

标签: asp.net asp.net-mvc iis iis-7 http-redirect

我在网站上有以下规则将http重定向到https。我们刚刚发现,虽然我们的应用程序只提交了api的http。在我们更新之前,我需要该网站忽略对/ api文件夹的调用,并仅重定向其他所有内容。我确定有一种方法可以说如果URL不包含/ api /然后重定向。

<rewrite>
  <rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
  </rules>
</rewrite>

2 个答案:

答案 0 :(得分:1)

如果在/ api应用程序或目录中放置单独的 Web.config文件,则可以覆盖适用于整个站点的任何规则。

查看本文中的提示#1,如果您有时间阅读全部内容:

http://weblogs.asp.net/jgalloway/archive/2012/01/17/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides.aspx

John Galloway的博客是IIS和ASP.NET所有内容的绝佳资源。

答案 1 :(得分:1)

添加类似于<add input="{R:0}" pattern="/api(/|$)(.*)" negate="true" />的条目,以便整个文件为:

<rewrite>
  <rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{R:0}" pattern="/api(/|$)(.*)" negate="true" />
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
  </rules>
</rewrite>

示例网址:http://site.com/api/function

因此,如果网站之后的网址与以下任何内容匹配,则会停止处理(因此不会将用户推送到https)

  • /api
  • /api/anything
  • 任何https网址

我们遇到同样的事情,在IIS后面的一个大型应用程序运行反向代理。 IIS的URL重写插件(您似乎正在使用它)有点痛苦,但它确实很好地完成了工作,并且容忍了MVC框架。

正如您所提到的,简单地在API目录中放置重写块是行不通的,因为MVC没有目录。你会认为MS会有更好的解决方案 - 但他们不会。它使事情变得更具挑战性。