asp.net整站301重定向方法,asp.net整站301重定向方法成功验证方法

asp.net整站301重定向方法,asp.net整站301重定向方法成功验证方法


asp.net整站301重定向方法

步骤1、使用web.config配置

找到节点<appSettings>  </appSettings>插入一个<add />节点"value=重定向到达的域名地址",如:我要定向到www.xustan.com

  <appSettings>
    <add key="301Redirect" value="www.xustan.com"/>
  </appSettings>

找到节点<system.web><httpModules>      </httpModules></system.web>插入一个<add />节点"type=下面步骤2建的类文件类名"

  <system.web>
    <httpModules>
      <add name="301Redirect" type="UrlRewrite" />
    </httpModules>
  </system.web>
 
 
步骤2、 在asp.net项目的App_Code文件夹里建立url重定向方法类UrlRewrite.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// UrlRewrite 的摘要说明
/// </summary>
public class UrlRewrite : IHttpModule
{
    public void Dispose()
    {
    }
    public void Init(HttpApplication context)
    {
        context.AuthorizeRequest += (new EventHandler(Process301));
    }
    public void Process301(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpRequest request = app.Context.Request;
        string lRequestedPath = request.Url.DnsSafeHost.ToString();
        string strWebURL = ConfigurationManager.AppSettings["301Redirect"].ToString();
        if (lRequestedPath.IndexOf(strWebURL) == -1)
        {
            app.Response.StatusCode = 301;
            app.Response.AddHeader("Location", lRequestedPath.Replace(lRequestedPath, "http://" + strWebURL + request.RawUrl.ToString().Trim()));
            app.Response.End();
        }
    }
}



asp.net整站301重定向方法成功验证方法

到站长工具 http://tool.chinaz.com/pagestatus/    输入验证,返回状态码:301 。则表示asp.net整站301重定向成功

猜你喜欢

转载自blog.csdn.net/ningxi_/article/details/6884663