【问题标题】:Url ReWrite http to https including wwwUrl 将 http 重写为 https,包括 www
【发布时间】:2018-07-05 12:24:56
【问题描述】:

页面应该使用 301 重定向从 http://example.com/... 重定向到 https://www.example.com/...。

页面应该使用 301 重定向从 http://www.example.com/... 重定向到 https://www.example.com/...。

页面应该使用 301 重定向从 https://example.com/... 重定向到 https://www.example.com/...。

我在 web.config 中添加了以下 URL 重写。

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

    <rule name="Redirect example.com to www" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="example.com" />
        <add input="{HTTPS}" pattern="on" />
      </conditions>
      <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent"/>
    </rule>

上面的代码不能正常工作。

【问题讨论】:

  • 您可能希望在第二条规则的 HTTPS 条件上使用 ignoreCase 属性。

标签: asp.net iis web-config sitecore


【解决方案1】:

试试这些规则,你也应该尝试重启 App Pool 以确保新规则被采纳。

<rule name="Redirect to HTTPS" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{HTTPS}" pattern="^OFF$" />
   </conditions>
 <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" />  
</rule>

<rule name="Redirect non-www to www" patternSyntax="Wildcard" stopProcessing="true">
   <match url="*" />
   <conditions>
     <add input="{HTTP_HOST}" pattern="example.com" />
   </conditions>
   <action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>

【讨论】:

    【解决方案2】:

    您的重定向应该可以正常工作,因为我刚刚尝试过。

    尝试重置您网站的应用程序池或终止相应的 w3wp.exe 进程,因为模块 URLrewrite 正在缓存设置,如果您不强制,有时可能需要很长时间才能使新更改/规则生效重置那个缓存。

    还要确保主机 www.example.com 和 example.com 都使用 http 和 https 位于具有上述规则的同一个 IIS 站点上。

    【讨论】:

      【解决方案3】:

      要考虑的另一件事是浏览器缓存。浏览器缓存 301 重定向,这意味着如果您在某些时候设置不正确,您的浏览器中将缓存错误的重定向。

      您需要清除浏览器中的重定向缓存才能解决此问题。

      以下是从 https://superuser.com/questions/304589/how-can-i-make-chrome-stop-caching-redirects 获取的从 Chrome 中清除的步骤

      Chrome 菜单 Chrome 菜单 > 设置 > 显示高级设置... > 隐私 > 点击清除浏览数据...

      无论您选择其他什么,请确保“缓存的图像和文件”是 选中的选项。

      然后单击清除浏览数据,您应该可以再次重新测试。

      如果你刚刚跟随重定向,你只需要删除数据 过去一小时。

      或者,在隐身模式下测试和开发。缓存在那里 关闭浏览器后刷新。

      【讨论】:

        猜你喜欢
        • 2014-12-16
        • 2017-12-04
        • 2016-05-08
        • 2011-09-15
        • 1970-01-01
        • 2015-01-10
        • 1970-01-01
        • 2015-06-18
        • 2014-09-20
        相关资源
        最近更新 更多