【问题标题】:IIS Http to Https when web app is within the site当 Web 应用程序位于站点内时,IIS Http 到 Https
【发布时间】:2026-01-29 10:10:01
【问题描述】:

我有这个重写规则:

<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 流量,但问题是我的 web 应用程序不在根文件夹中,所以它是这样的:

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

然后重定向到:

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

...而不是

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

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

【问题讨论】:

    标签: iis url-rewriting iis-7


    【解决方案1】:

    这对我有用:

    <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}。这允许在没有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>
    

    【讨论】:

    • 当然。您可以删除该行。我们配置的这一部分。我们有几个没有证书的网站。
    最近更新 更多