Asp.net IIS 7上的条件https重定向

Asp.net IIS 7上的条件https重定向,asp.net,asp.net-mvc,iis,iis-7,http-redirect,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="(.*)

我在站点上有以下规则将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>

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

查看本文中的技巧1,如果您有时间,请全部阅读:


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

添加一个类似于
的条目,这样整个文件就是:

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

示例URL:
http://site.com/api/function

因此,如果站点后的URL与以下任何一项匹配,它将停止处理(因此不会将用户推送到https)

  • /api
  • /api/anything
  • 任何
    https
    URL
我们遇到了同样的情况,一个大型应用程序运行在IIS中的反向代理后面。IIS的URL重写插件(您似乎正在使用)有点麻烦,但它做得非常好,并且可以容忍MVC框架


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

除了这是一个MVC应用程序之外,它还可以工作,所以技术上没有API目录。我明白了!作为一个快速解决方案,您可以在“匹配url”字段中使用正则表达式,并编写特定于API的规则。这就是我的想法,我只是不知道如何编写正则表达式部分。但我希望其他人有一个更优雅的MVC长期解决方案;)