ASP.NET MVC 3 技術(九) 301永久重新導向不帶www網域名稱到帶www的網域名稱

來源:互聯網
上載者:User

標籤:oid   query   ons   class   string   http   app   rect   設定   

在 .net 4 中實現永久重新導向非常容易,可以參考ASP.NET MVC3 技術(四) 永久重新導向方法。今天主要說明下怎麼在 asp.net mvc 3 實現從帶www的網域名稱永久重新導向到不帶www的網域名稱,站長這樣做也是為了提高網域名稱的排名。

一些朋友可能會使用比較苯的方法,在每個 Controller 下都加上 Response.RedirectPermanent 方法。較為簡單的做法是在 Global.axax.cs 中加入下邊的代碼:

protected void Application_BeginRequest(object sender, EventArgs e){ string strUrl = Request.Url.ToString().Trim().ToLower(); if (strUrl.Contains("http://lidongkui.com")) { Response.RedirectPermanent("http://www.lidongkui.com"); //not good }}

好果你比較認真的話,會發現這個寫法有些不太友好,下邊的寫法顯得更好些:

Response.RedirectPermanent(strUrl.Replace("http://lidongkui.com", "http://www.lidongkui.com")); //good

本篇內容非常簡單,但很多人在處理永久重新導向時的做法卻是錯誤的,希望對大家有所協助,你或許有興趣瞭解下網域名稱要不要www、asp.net 如何正確設定404頁面

ASP.NET MVC 3 技術(九) 301永久重新導向不帶www網域名稱到帶www的網域名稱

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.