asp.net mvc:如何将非www重定向到www,反之亦然

Luk*_*101 43 c# iis asp.net-mvc-2

我想将所有www流量重定向到非www流量

我已将其复制到我的web.config中

<system.webServer> / <rewrite> / <rules> 

<rule name="Remove WWW prefix" > 
<match url="(.*)" ignoreCase="true" /> 
<conditions> 
<add input="{HTTP_HOST}" pattern="^www\.domain\.com" /> 
</conditions> 
<action type="Redirect" url="http://domain.com/{R:1}" 
    redirectType="Permanent" /> 
</rule> 
Run Code Online (Sandbox Code Playgroud)

根据这篇文章

如何使用"www"URL重定向到没有"www"URL,反之亦然?

但我得到了500内部服务器错误.

小智 76

您可以考虑采用不同的方法:

protected void Application_BeginRequest (object sender, EventArgs e)
{
   if (!Request.Url.Host.StartsWith ("www") && !Request.Url.IsLoopback)
   {
      UriBuilder builder = new UriBuilder (Request.Url);
      builder.Host = "www." + Request.Url.Host;
      Response.Redirect (builder.ToString (), true);
   }
}
Run Code Online (Sandbox Code Playgroud)

但是,这将进行302重定向,因此建议进行一些调整:

protected void Application_BeginRequest (object sender, EventArgs e)
{
   if (!Request.Url.Host.StartsWith ("www") && !Request.Url.IsLoopback)
   {
      UriBuilder builder = new UriBuilder (Request.Url);
      builder.Host = "www." + Request.Url.Host;
      Response.StatusCode = 301;
      Response.AddHeader ("Location", builder.ToString ());
      Response.End ();
   }
}
Run Code Online (Sandbox Code Playgroud)

这个将返回301 Moved Permanently.

  • @jakubka:我认为你实际上打算输入Response.RedirectPermanent.你的链接是正确的. (3认同)
  • 它在Global.asax.cs中. (2认同)
  • 从.NET 4.0开始,有[Response.ResponsePermanent](http://msdn.microsoft.com/en-us/library/system.web.httpresponse.redirectpermanent(v = vs.100).aspx)方法生成代码比手动设置标题更简单 (2认同)

Cha*_*ell 12

如果你直接复制它,那么你的web.config中有不正确的标记

你需要

<system.webServer> 
    <rewrite>
      <rules>
        <rule name="Remove WWW prefix" > 
        <match url="(.*)" ignoreCase="true" /> 
        <conditions> 
        <add input="{HTTP_HOST}" pattern="^www\.domain\.com" /> 
        </conditions> 
        <action type="Redirect" url="http://domain.com/{R:1}" 
            redirectType="Permanent" /> 
        </rule> 
      </rules>
    </rewrite>
<system.webServer>
Run Code Online (Sandbox Code Playgroud)

这条线说

<system.webServer> / <rewrite> / <rules> 
Run Code Online (Sandbox Code Playgroud)

表示您需要将配置放在web.Config中的该位置.
<system.webServer>是您的web.Config文件的configSections之一.

编辑:

确保首先为IIS7安装了URL Rewrite模块

上面的页面讨论了将HTTP重定向到HTTPS,但这个概念仍适用于WWW到非WWW

此外,这里有一些关于它们如何结合在一起的详细信息.


z3n*_*d3v 8

    **For a www to a non www Thanks @developerart**

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (Request.Url.Host.StartsWith("www") && !Request.Url.IsLoopback)
        {
            UriBuilder builder = new UriBuilder(Request.Url);
            builder.Host = Request.Url.Host.Replace("www.","");
            Response.StatusCode = 301;
            Response.AddHeader("Location", builder.ToString());
            Response.End();
        }
    }
Run Code Online (Sandbox Code Playgroud)


Kon*_*kus 5

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (!this.Request.Url.Host.StartsWith("www") && !this.Request.Url.IsLoopback)
    {
        var url = new UriBuilder(this.Request.Url);
        url.Host = "www." + this.Request.Url.Host;
        this.Response.RedirectPermanent(url.ToString(), endResponse: true);
    }
}
Run Code Online (Sandbox Code Playgroud)