【问题标题】:Rewrite rule to WWW except when on localhost将规则重写为 WWW,除非在本地主机上
【发布时间】:2014-07-23 20:26:51
【问题描述】:

出于 SEO 优化的原因,我将所有对 http://Example.com 的请求重定向到 http://www.Example.com。问题是在本地工作时,对localhost 的请求也会被重定向。

我尝试了这个Rewrite rule to HTTPS except when on localhost 答案中的建议,但没有成功。

这是我位于Web.Config 的实际重定向规则(希望它可以帮助正在寻找重写规则到 WWW 的人):

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="redirect example.com to www.example.com">
          <match url="^(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
          </conditions>
          <action type="Redirect" url="http://www.example.com/{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

有什么帮助吗?

【问题讨论】:

    标签: asp.net url-rewriting web-config


    【解决方案1】:

    你可以做如下规则

    <rule name="redirect example.com to www.example.com">
       <match url=".*" />
          <conditions logicalGrouping="MatchAll">
              <add input="{HTTP_HOST}" pattern="^www.*" negate="true" />
              <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
          </conditions>
          <action type="Redirect" url="http://www.example.com/{R:0}" />
       </rule>
    </rules>
    

    它将两个条件与“MatchAll”结合在一起,其中第一个输入类似于您已有的输入(如果需要,您可以使用您的),第二个是检查本地主机。请注意,您可能需要使用 {R:0} 并且我还将匹配 url 更改为 .*

    您还可以使用不同的 web.config(调试和发布)。在这里阅读更多:http://msdn.microsoft.com/en-us/library/vstudio/dd465318(v=vs.100).aspx

    【讨论】:

    • 我想保留一个单一但更智能的 web.config,它更不容易出错。不幸的是,您的解决方案不起作用,localhost:3064 仍然被重定向到 www.example.com
    猜你喜欢
    • 2014-01-23
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 2018-05-06
    • 1970-01-01
    • 2016-10-07
    • 2014-11-30
    相关资源
    最近更新 更多