【问题标题】:Force HTTPS via URL rewrite in web.config通过 web.config 中的 URL 重写强制 HTTPS
【发布时间】:2014-04-15 06:46:54
【问题描述】:

我正在尝试通过包含路径的 URL 的 web.config URL 重写规则强制使用 HTTPS。

例如http://my.domain.com/folder必须强制使用https://my.domain.com/folder

我还想在其相对路径上强制所有页面使用 HTTPS

例如 http://my.domain.com/folder/page.aspx 强制 https://my.domain.com/folder/page.aspx

这就是我所拥有的:

<rewrite>
    <rules>
      <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>
    </rules>
  </rewrite>

【问题讨论】:

    标签: iis azure url-rewriting web-config


    【解决方案1】:

    检查您的大小写和您使用的引号类型。此外,第二种输入法无效 - 模式不是有效的正则表达式。此规则适用于您想要的:

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

    【讨论】:

    • 谢谢!我尝试过类似的方法,但经验是它重定向到 https://my.domain.com 而没有尾随路径
    【解决方案2】:

    试试这个 URL 重写规则:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAny">
              <add input="{SERVER_PORT_SECURE}" pattern="^0$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    

    【讨论】:

      猜你喜欢
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2017-05-11
      • 1970-01-01
      • 2011-08-25
      • 2012-10-11
      • 1970-01-01
      相关资源
      最近更新 更多