【问题标题】:Redirect non www to www rule causing issue for files将非 www 重定向到 www 规则导致文件问题
【发布时间】:2012-04-11 10:27:01
【问题描述】:

我已经在我的 web.config 中添加了这条规则来将非 www 的 URL 重定向到 www。

 <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^example.com$" />
          </conditions>
          <action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
        </rule>

虽然它适用于主站点 URL。就像用户输入 http://example.com 一样,它会重定向到 http://www.example.com

但是对于一些像

这样的 URL

http://www.example.com/userfiles/abc.pdf

它重定向到

http://www.www.example.com/userfiles/abc.pdf

在这里你可以在 URL 中看到 2 次 www。

【问题讨论】:

    标签: asp.net iis redirect web-config rules


    【解决方案1】:

    我想这个应该可以:

    <rule name="Redirect to WWW" stopProcessing="true">
      <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>
    

    如果这个没有按预期工作,您可以尝试添加另一个条件:

    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 2017-05-09
      • 2015-10-12
      • 1970-01-01
      • 2011-12-31
      • 2019-02-04
      相关资源
      最近更新 更多