WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

本篇内容非常简单,但很多人在处理永久重定向时的做法却是错误的,希望对大家有所帮助,你或许有兴趣了解下域名要不要wwwasp.net 如何正确设置404页面