【问题标题】:Web.config URL Rewrites - HTTPS and Non-WWWWeb.config URL 重写 - HTTPS 和非 WWW
【发布时间】:2016-09-15 13:08:10
【问题描述】:

我需要同时重写httpsnon-www,同时还需要不对域进行硬编码,因为我们有很多服务器。这需要在web.config 中,而不是在IIS 中。

我读过很多文章:

https 重写有效,non-www 无效。

    <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>
    <rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <!--<add input="{CACHE_URL}" pattern="*://www.*" />-->
        <!--<add input="{HTTP_HOST}" pattern="*://www.*" />-->
        <add input="{HTTP_HOST}" pattern="^.*www.*" />
      </conditions>
      <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
      // i've also tried
      // url="{C:2}/{R:1}"
      // url="{C:1}/{C:2}"
    </rule>

我在正则表达式测试器上测试了 ^.*www.* 的正则表达式,它匹配 www.testing.com 但不匹配 testing.com - 所以我认为该模式会捕获它。

我需要重定向的 URL:

【问题讨论】:

    标签: asp.net regex iis url-rewriting


    【解决方案1】:

    是我自己的问题 - www 没有 DNS,因此重定向无法自行解决。

    使用的代码:

        <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>
        <rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
          <match url="*" />
          <conditions>
            <add input="{CACHE_URL}" pattern="*://www.*" />
          </conditions>
          <action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
        </rule>
    

    【讨论】:

      【解决方案2】:

      添加“规范域名”规则。

      Rob 发现的解决方案也可以正常工作。恕我直言,我的似乎更容易一些。

      【讨论】:

        猜你喜欢
        • 2017-12-04
        • 2014-12-16
        • 2018-03-31
        • 2017-07-13
        • 1970-01-01
        • 2018-06-30
        • 1970-01-01
        • 2023-03-14
        • 2018-07-03
        相关资源
        最近更新 更多