2010-07-07 19 views
40

我想所有的WWW流量重定向到非www交通asp.net的MVC:如何將非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> 

每這篇文章

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

,但我得到了500內部服務器錯誤。

回答

73

你可能會考慮不同的方法:

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

這一次將返回301永久移動。用戶

+0

這種方法在MVC中工作嗎?你的方式是我如何在Web窗體中完成它,但我認爲MVC路由框架的處理方式不同。 – 2010-07-07 17:58:37

+1

我採取了非常類似的方法。請參閱http://stackoverflow.com/questions/2175975/asp-net-mvc-301-redirect-from-www-domain-com-to-domain-com – spender 2010-07-07 18:06:17

+0

@rockinthesixstring:它在MVC中起作用。它在很早的時候就開始了,所以MVC或者WebForms是否會在之後處理請求並不重要。 – 2010-07-07 18:32:10

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> 

,說

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

是說明你需要把線在您的web.Config內的該位置的配置。
<system.webServer>是您的web.config文件的configSections之一。

編輯:

請務必先安裝IIS7

上述有關重定向HTTP到HTTPS會談的頁面URL Rewrite module,但概念仍然適用於WWW非WWW

而且,這裏有一些detailed information關於它是如何組合在一起的。

+0

ok..i已經做到了這一點,它說不是一個有效的標籤 – Luke101 2010-07-07 18:06:03

+0

還是讓我看看吧。 – 2010-07-07 18:29:21

+0

我編輯了我的答案。我仍在研究如何使Visual Studio不會對你大叫,但它應該適用於IIS7 – 2010-07-07 18:55:28

6
**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(); 
     } 
    } 
+0

nope,請求的URL「/」無效。 – Toolkit 2016-03-24 19:43:56

2

大廈151323的回答,這裏完整的答案誰也希望阻止用戶從一個azurewebsites.net子域名訪問網站Azure的用戶(這個進入您的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(); 
     } 
    } 
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); 
    } 
} 
0

我知道這個線程是古老的,似乎是回答死亡。但是,將每個人的global.asax建議與檢查你是否在本地工作或者不在同一個地方。這種方式在使用IIS Express進行開發期間,您的站點不會嘗試重定向到「www」子域。沿着線

東西:

protected void Application_BeginRequest(
     object sender, 
     EventArgs e) 
    { 
     if (!Request.IsLocal) 
     { 
      // Do your check for naked domain here and do permanent redirect 
     } 
    } 
1

您可以使用HTTPS和www重定向。(您需要更改「例如」)

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