代码之家  ›  专栏  ›  技术社区  ›  Luke101

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

  •  40
  • Luke101  · 技术社区  · 14 年前

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

    <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> 
    

    根据这篇文章

    How to redirect with "www" URL's to without "www" URL's or vice-versa?

    但我有一个500的内部服务器错误。

    7 回复  |  直到 7 年前
        1
  •  85
  •   user151323 user151323    14 年前

    您可以考虑另一种方法:

    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);
       }
    }
    

    不过,这将执行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 ();
       }
    }
    

    这个会永久性的返回。

        2
  •  13
  •   Chase Florell    14 年前

    如果您直接复制了它,那么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>
    

    上面写着

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


    <system.webServer>

    确保你先有 URL Rewrite module

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

    还有,这里有一些 detailed information

        3
  •  8
  •   z3nd3v    12 年前
        **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();
            }
        }
    
        4
  •  5
  •   Konstantin Tarkus    11 年前
    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);
        }
    }
    
        5
  •  3
  •   Serj Sagan    8 年前

    以用户151323的答案为基础,这里给出了Azure用户的完整答案,这些用户还希望阻止用户从 azurewebsites.net 子域(这进入你的 Global.asax MvcApplication 对于MVC用户):

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Request.Url.Host.StartsWith("YourSite.azurewebsites") && !Request.Url.IsLoopback)
            {
                Response.StatusCode = 301;
                Response.AddHeader("Location", "www.YourSite.com");
                Response.End();
    
                return;
            }
    
    
            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();
            }
        }
    
        6
  •  3
  •   Ergin Çelik    7 年前

    <system.webServer>
      <!-- For force ssl and www -->
      <rewrite>
        <rules>
          <!-- For force ssl -->
          <rule name="http to https" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
              <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
          </rule>
          <!-- For force ssl -->
          <!-- For force www -->
          <rule name="redirect to www" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
              <add input="{HTTP_HOST}"  pattern="^example\.com$" />
            </conditions>
            <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" redirectType="Permanent" />
          </rule>
          <!-- For force www -->
        </rules>
      </rewrite>
      <!-- For force ssl and www -->
    </system.webServer>
    
        7
  •  1
  •   Sarel Esterhuizen    9 年前

    protected void Application_BeginRequest(
            object sender,
            EventArgs e)
        {
            if (!Request.IsLocal)
            {
                // Do your check for naked domain here and do permanent redirect
            }
        }
    
        8
  •  0
  •   Manjunath Bilwar    3 年前

    此规则将起作用,但请注意,只有在IIS服务器上安装了URL重写时,此规则才起作用。如果您的IIS服务器上未安装URL重写,则此规则将不起作用。

    要在服务器上安装URL重写,请访问此官方网站 link 在服务器上下载并安装扩展。然后重启IIS服务器,它就可以工作了。

    URL重写扩展官方网站链接: https://www.iis.net/downloads/microsoft/url-rewrite

    <rewrite>
                <rules>
                    <rule name="Redirect yourdomainname.com to http://www.yourdomainname.com HTTP" patternSyntax="ECMAScript" stopProcessing="true">
                        <match url=".*"></match>
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^yourdomainname.com$"></add>
                            <add input="{HTTPS}" pattern="off"></add>
                        </conditions>
                        <action type="Redirect" url="http://www.yourdomainname.com/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
                    </rule>
                </rules>
            </rewrite>