【问题标题】:Redirect non-www to www Results in Page isnt Redirecting Properly将非 www 重定向到 www 结果页面未正确重定向
【发布时间】:2014-03-19 05:40:23
【问题描述】:

我正在尝试将非 www 网站重定向到 www,但是当我在 web.config 或 Global.asax 中添加重定向规则时,它显示该页面未正确重定向。 web.config 中的重定向规则

    <rewrite>
      <rules>
        <rule name="CanonicalHostNameRule" enabled="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern="testdomain\.com$" />
            <add input="{HTTP_HOST}" pattern="^www\.testdomain\.com$" negate="true" />
          </conditions>
          <action type="Redirect" url="http://www.testdomain.com/{R:1}" />
        </rule>
      </rules>
    </rewrite>

我也从Application_BeginRequest 中的 Global.asax 尝试过这个 注意:我一次尝试这些事情之一。

if(HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://testdomain.com"))
{
    HttpContext.Current.Response.Status = "301 Moved Permanently";
    HttpContext.Current.Response.AddHeader("Location",
    Request.Url.ToString().ToLower().Replace("http://testdomain.com", "http://www.testdomain.com"));
}

我的 Default.aspx 和 Default.aspx.cs 页面中没有代码.. 任何想法的家伙,实际问题可能出在哪里。

【问题讨论】:

    标签: asp.net .net iis web-config url-redirection


    【解决方案1】:

    将此代码添加到&lt;system.webServer&gt;部分下

    <rewrite>
    <rule name="Redirect to www">
      <match url=".*" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
      </conditions>
      <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" redirectType="Permanent"/>
    </rule>
    </rewrite>
    

    【讨论】:

    • 是的,我一次只使用一件事。我首先尝试了 web.config ruie。然后在我尝试了 global.asax
    猜你喜欢
    • 2021-05-07
    • 2018-11-25
    • 2018-06-06
    • 1970-01-01
    • 2018-07-18
    • 2017-11-24
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多