protected void Application_BeginRequest(Object sender, EventArgs e)
{
string strRawUrl = HttpContext.Current.Request.RawUrl;
string strNewUrl;
//*******************************************************************************************
//分类地址重写
if (Regex.IsMatch(strRawUrl, @"Post.aspx", RegexOptions.IgnoreCase))
{
strNewUrl = Regex.Replace(strRawUrl, @"Post.aspx", @"ShowClass.aspx\?ClassID=14");
HttpContext.Current.RewritePath( strNewUrl );
}
else if (Regex.IsMatch(strRawUrl, @"Resources.aspx", RegexOptions.IgnoreCase))
{
strNewUrl = Regex.Replace(strRawUrl, @"Resources.aspx", @"ShowClass.aspx\?ClassID=13");
HttpContext.Current.RewritePath( strNewUrl );
}
else if (Regex.IsMatch(strRawUrl, @"MobileApplications.aspx", RegexOptions.IgnoreCase))
{
strNewUrl = Regex.Replace(strRawUrl, @"MobileApplications.aspx", @"ShowClass.aspx\?ClassID=12");
HttpContext.Current.RewritePath( strNewUrl );
}
else if (Regex.IsMatch(strRawUrl, @"Server.aspx", RegexOptions.IgnoreCase))
{
strNewUrl = Regex.Replace(strRawUrl, @"Server.aspx", @"ShowClass.aspx\?ClassID=11");
HttpContext.Current.RewritePath( strNewUrl );
}
else if (Regex.IsMatch(strRawUrl, @"DataBase.aspx", RegexOptions.IgnoreCase))
{
strNewUrl = Regex.Replace(strRawUrl, @"DataBase.aspx", @"ShowClass.aspx\?ClassID=10");
HttpContext.Current.RewritePath( strNewUrl );
}
else if (Regex.IsMatch(strRawUrl, @"WebServices.aspx", RegexOptions.IgnoreCase))
{
strNewUrl = Regex.Replace(strRawUrl, @"WebServices.aspx", @"ShowClass.aspx\?ClassID=7");
HttpContext.Current.RewritePath( strNewUrl );
}
else if (Regex.IsMatch(strRawUrl, @"WindowsApplications.aspx", RegexOptions.IgnoreCase))
{
strNewUrl = Regex.Replace(strRawUrl, @"WindowsApplications.aspx", @"ShowClass.aspx\?ClassID=6");
HttpContext.Current.RewritePath( strNewUrl );
}
else if (Regex.IsMatch(strRawUrl, @"WebApplications.aspx", RegexOptions.IgnoreCase))
{
strNewUrl = Regex.Replace(strRawUrl, @"WebApplications.aspx", @"ShowClass.aspx\?ClassID=2");
HttpContext.Current.RewritePath( strNewUrl );
}
//**********************************************************************************************
//显示详细文章地址重写
else if (Regex.IsMatch(strRawUrl, @"(\d+).aspx", RegexOptions.IgnoreCase))
{
strNewUrl = Regex.Replace(strRawUrl, @"(\d+).aspx", @"ShowPost.aspx\?id=$1");
HttpContext.Current.RewritePath( strNewUrl );
}
else if (Regex.IsMatch(strRawUrl, @"ShowPost.aspx\?id=(\d+)", RegexOptions.IgnoreCase))
{
strNewUrl = Regex.Replace(strRawUrl, @"ShowPost.aspx\?id=(\d+)", @"$1.aspx");
HttpContext.Current.Response.Redirect( strNewUrl );
}
}
前面是重写。后面是逆操作.
效果见HTtp://ithero.net