【问题标题】:Url rewrite rules not redirecting网址重写规则不重定向
【发布时间】:2019-02-04 08:00:03
【问题描述】:

我的 .net mvc 网站托管在 Liquid Web 云中。如果他们输入以下网址http://example.com,http://www.example.com,https://example.com

,我想重定向所有网站用户

https://www.example.com(即一致的网址,无论他们如何输入) 我在 web.config 中尝试了以下代码,但没有运气。

    <rule name="Redirect Non WWW" stopProcessing="true" > 
    <match url="^(http\.)(.*)$" />  
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="^OFF$" />  
         <add input="{HTTP_HOST}" pattern="^https://www.example.com$" />  

          </conditions>
          <action type="Redirect" redirectType="Permanent" url="https://www.example.com/{R:0}" appendQueryString="true" />
        </rule>

域已经注册为 www.example.com 并且在域上启用了 SSL。

【问题讨论】:

  • > 无效

标签: url-rewriting


【解决方案1】:

你已经使用了MatchAll 并且还设置了url 应该有https 的条件来开始重定向!只需将其更改为 http

不正确:

 <add input="{HTTP_HOST}" pattern="^https://www.example.com$" /> 

正确:

 <add input="{HTTP_HOST}" pattern="^http://www.example.com$" />

【讨论】:

    【解决方案2】:

    由于您要强制重定向使用 HTTPS,因此 (.*) 将匹配所有 URL,您可以将模式设置为关闭。

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

    更新
    方法1没有问题,不过也给以后看这篇文章的人补充一下。

    方法二:

    <rule name="HTTP to HTTPS Redirection" enabled="true">
        <match url="(.*)" />
        <action type="Redirect" url="https://www.example.com/{REQUEST_URI}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    

    【讨论】:

    • 也许你已经设置了另一个重定向规则,删除所有其他重定向,然后重试
    • 我只有一条规则,如果我们不提供除 https 模式关闭之外的任何条件,那么重定向的 url 也会匹配相同的规则 (.*),因此会导致多次重定向。
    • 我提交的样本来自官方文档,它可以工作,这是链接:blogs.technet.microsoft.com/dawiese/2016/06/07/…
    • 无论如何在条件中添加这一行,许多使用 aws 的用户发现这很有用&lt;add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" /&gt;
    • 我也试过了 >
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多