【问题标题】:URL Rewrite on IIS: HTTP to HTTPS rule to also include non-www to www redirectIIS 上的 URL 重写:HTTP 到 HTTPS 规则还包括非 www 到 www 重定向
【发布时间】:2016-05-08 17:21:30
【问题描述】:

如何修改此规则以包括非 www 到 www 重定向?

    <rule name="Force Https" stopProcessing="true">
      <match url="healthcheck.html" negate="true" />
      <conditions>
        <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
    </rule>

有没有更好的方法来强制全站使用 HTTPS?我在 IIS 8.5 上使用 ASP.NET MVC 5

【问题讨论】:

    标签: asp.net iis https url-rewriting


    【解决方案1】:

    我认为您只需要添加另一个规则来检查 HTTP_HOST 变量是否仅包含您的主机而没有 www. 前缀,如果是则重定向:

    <!-- You first rule. Note stopProcessing is now false. -->
    <rule name="Force Https" stopProcessing="false">
      <match url="healthcheck.html" negate="true" />
      <conditions>
        <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
    </rule>
    
    <!-- Additional rule. -->
    <rule name="Force WWW" stopProcessing="true">
      <match url="healthcheck.html" negate="true" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^yourdomain\.com"/>
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}{REQUEST_URI}" />
    </rule>
    

    只需将上面的yourdomain.com 更改为您的实际主机域名即可。

    为了回答您的其他问题,我认为 URL 重定向(通过 URL 重写)是强制 HTTPS 而不向仍尝试通过 HTTP 访问您的网站的用户返回 403 的最简单方法。

    更新

    针对您对双 301 的评论,您可以尝试此单一规则。我家里没有笔记本电脑可以验证,但我认为这会起作用:

    <!-- This rule will capture any http request regardless of the 'www.' prefix in the URL -->
    <rule name="Force Https" stopProcessing="true"> 
      <match url="healthcheck.html" negate="true" />
      <conditions>
        <add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" />
      </conditions>
      <action type="Redirect" url="https://www.yourdomain.com{REQUEST_URI}" redirectType="Permanent" />
    </rule>
    <!-- This rule will capture https request that does not have the 'www.' prefix in the URL -->
    <rule name="Force WWW Prefix" stopProcessing="true">
      <match url="healthcheck.html" negate="true" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" />
        <add input="{HTTP_HOST}" pattern="^yourdomain\.com$"/>
      </conditions>
      <action type="Redirect" url="https://www.yourdomain.com{REQUEST_URI}" redirectType="Permanent" />
    </rule>
    

    【讨论】:

    • 这种方式可行,但是在导航到 http:// 时它会提供两个 301 重定向,我宁愿只有一个
    • 获取重定向循环
    • @N0ir,抱歉,您的评论很有道理。试试我的更新到我的更新。 :)
    • @N0ir,请将HTTP_HOST 模式更改为^yourdomain\.com$,并将第一条规则上的HTTP_X_FORWARDED_PROTO 更改为^http$(根据我的更新)。
    • 当您尝试 301 循环时,请在 Chrome 中使用隐身模式,否则您需要不断清除浏览器历史记录,因为 301 已被缓存。
    猜你喜欢
    • 2019-02-04
    • 2020-11-28
    • 2013-06-11
    • 2018-07-25
    • 2019-12-13
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    相关资源
    最近更新 更多