繁体   English   中英

通过在web.config中重写URL强制HTTPS

[英]Force HTTPS via URL rewrite in web.config

我正在尝试通过web.config URL重写规则对包含路径的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>

检查外壳和所用报价的类型。 另外,第二个输入法无效-模式对正则表达式无效。 该规则适用于您想要的:

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

尝试以下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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM