【问题标题】:Redirect from www to none-www not working从 www 重定向到非 www 不起作用
【发布时间】:2021-09-16 15:00:12
【问题描述】:

该应用程序基于 asp.net core 3.1 构建并做出反应。它们同源。

web.config 中从 http 重定向到 https working

web.config 中从 www.example.com 重定向到 example.com 不起作用

        // Piece of my web.config
        <rules>
          <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:1}" />
          </rule>
          <rule name="RedirectWwwToNonWww" stopProcessing="false">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
              <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
            </conditions>
            <action type="Redirect" url="https://{C:2}{REQUEST_URI}" redirectType="Permanent" />
          </rule>
        </rules>

  • 在浏览器中www.example.com 进入重定向循环并显示 ERR_TOO_MANY_REDIRECTS
  • 在 Postman 中,错误为 Error: Exceeded maxRedirects. Probably stuck in a redirect loop https://www.example.com/

【问题讨论】:

    标签: asp.net asp.net-core iis asp.net-web-api web-config


    【解决方案1】:

    您可以尝试使用此规则从 www 重定向到 none-www:

    <rule name="SecureRedirect" stopProcessing="true">
      <match url="^(.*)$" />
        <conditions>
          <add input="{HTTPS}" pattern="off" />
          <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
        </conditions>
      <action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
    </rule>
    

    【讨论】:

    • 泰。在 1 条规则而不是 2 条规则时工作。我猜这两条规则相互循环。这是合乎逻辑的,但我还没有找到这样的信息。我想现在的想法是只有 1 个输入和 1 个操作(重定向)?
    • 最后我会从 none-www 重定向到 www。条件和行动是什么?
    • url规则的执行顺序是从上到下。如果你想从none-www重定向到www,可以参考这个链接:https://weblogs.asp.net/owscott/iis-url-rewrite-rewriting-non-www-to-www
    猜你喜欢
    • 2023-04-02
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多