【问题标题】:Force HTTPS on Azure and append URL在 Azure 上强制使用 HTTPS 并附加 URL
【发布时间】:2014-09-30 00:05:08
【问题描述】:

我正在尝试使用 web.config URL 重写规则在 Azure 网站上强制 HTTP 到 HTTPS。我想要一个包含以下两种情况的规则:

HTTP: //site.domain.com/sitefolder 到 HTTPS: //site.domain.com/sitefolder

HTTP: //site.domain.com/sitefolder/default.aspx 转 HTTPS: //site.domain.com/sitefolder/default.aspx

如果我关注this guide,我可以强制将 HTTP 转换为 HTTPS,但 URL 会更改为 HTTPS://site.domain.com,而没有附加 /sitefolder 或 /sitefolder/default.aspx。

这就是我目前所拥有的。强制使用 HTTPS,但不包括完整的 URL:

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

【问题讨论】:

    标签: azure url-rewriting


    【解决方案1】:

    这是在 Azure 网站上正确重定向到 HTTPS 的简单规则:

       <rule name="Redirect to https">
          <match url="(.*)"/>
          <conditions>
            <add input="{HTTPS}" pattern="Off"/>
            <add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
        </rule>
    

    您可以在这里测试它:http://double.azurewebsites.net(请注意链接中的 HTTP)我这里没有定义任何其他规则。上面的规则正确地重定向到深层链接,即http://double.azurewebsites.net/Home/About

    【讨论】:

    • @MK8 考虑到它只用于 head 和 get(意味着其他请求不会被重新路由到 HTTPS,如 POST)
    • 很多关于此的博客文章,但这是唯一有效的规则。
    • 至少对我来说,在天蓝色的网站上,这不起作用。此外,提供的示例链接会将您带到“http”网站而不是“https”......还有其他想法吗?
    • 完美运行!但是...如何修改 &lt;match&gt; 使其仅适用于 azurewebsites.net 而不适用于 localhost?
    • 回答我自己,添加这个:&lt;add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost(:\d+)?$" negate="true" /&gt; &lt;add input="{HTTP_HOST}" matchType="Pattern" pattern="^127\.0\.0\.1(:\d+)?$" negate="true" /&gt;
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 2019-01-23
    • 2018-09-18
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2015-06-25
    相关资源
    最近更新 更多