当 Web 应用程序位于站点内时,IIS Http 到 Https

IIS Http to Https when web app is within the site

我有这个重写规则:

<rewrite>
       <rules>
    <rule name="Redirect to http" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
        <match url="*" negate="false" />
        <conditions logicalGrouping="MatchAny">
            <add input="{HTTPS}" pattern="off" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
    </rule>
</rules>
    </rewrite>

我想将所有 http 流量重定向到 https 流量,但问题是我的网络应用程序不在根文件夹中,所以它是这样的:

http:\my-site.com\MyWebApp\some-parameters-here

在重定向后它会转到:

https:\my-site.com\some-parameters-here

...而不是

https:\my-site.com\MyWebApp\some-parameters-here

如何在不硬编码 URL 的情况下修复此重定向?

对我有用:

<rule name="HTTP to HTTPS redirect" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll"  trackAllCaptures="false">
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_HOST}" pattern="^((.+)?my-site\.com)$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />

关键是替换:{R:1}。这允许保持完整 link 没有 HTTP_HOST

更新 关于您的评论

您可以使用:

   <rule name="HTTP to HTTPS redirect" stopProcessing="true">
   <match url="(.*)" />
   <conditions>
   <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
     <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" 
      redirectType="Permanent" />
   </rule>