第一步:
新建一个网站,在网站中添加一个项目,用来做地址重写。
第二步:
webconfig中配置
<system.web>
<httpModules>
<add type="HttpHandler.URLRewriter.RewriterModule, HttpHandler" name="RewriterModule"/>
</httpModules>
</system.web>
第三步:
配置iis,属性-->主目录-->选择好选项-->点击配置-->添加-->.htm为静态页面后缀名,可以任意改为其它的,其它的两个文本框则和.aspx里的配置一样
第四步:
如果为80端口,则修改host配置文件
C:\WINDOWS\system32\drivers\etc\hosts
新增:127.0.0.1 url.rewrite.com
主机头为127.0.0.1 url.rewrite.com里面的(url.rewrite.com)保持一致
完成后访问地址:http://url.rewrite.com/Default-id-3.htm
----------------------------------------下为重新地址的代码
using System;
using System.Collections.Specialized;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;
/// <summary>
/// FormRewriter 的摘要说明
/// </summary>
namespace HttpHandler.URLRewriter.Form
{
public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
{
public FormRewriterControlAdapter()
{
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(new RewriteFormHtmlTextWriter(writer));
}
}
public class RewriteFormHtmlTextWriter : HtmlTextWriter
{
public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
: base(writer)
{
base.InnerWriter = writer.InnerWriter;
}
public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
: base(writer)
{
base.InnerWriter = writer;
}
public override void WriteAttribute(string name, string value, bool fEncode)
{
//If the attribute we are writing is the "action" attribute, and we are not on a sub-control,
//then replace the value to write with the raw URL of the request - which ensures that we'll
//preserve the PathInfo value on postback scenarios
if (name == "action")
{
HttpContext context = HttpContext.Current;
if (context.Items["ActionAlreadyWritten"] == null)
{
//We will use the Request.RawUrl property within ASP.NET to retrieve the origional
//URL before it was re-written.
value = context.Request.RawUrl;
//Indicate that we've already rewritten the <form>'s action attribute to prevent
//us from rewriting a sub-control under the <form> control
context.Items["ActionAlreadyWritten"] = true;
}
}
base.WriteAttribute(name, value, fEncode);
}
}
}
namespace HttpHandler.URLRewriter
{
public class RewriterModule : IHttpModule
{
public void Init(HttpApplication app)
{
// WARNING! This does not work with Windows authentication!
// If you are using Windows authentication, change to app.BeginRequest
//app.AuthorizeRequest += new EventHandler(this.CheckInject);//安全字符检查
app.AuthorizeRequest += new EventHandler(this.URLRewriter);//URL地址重写
app.AcquireRequestState += new EventHandler(CheckUserLogin);
}
#region 地址重写
/// <summary>
/// 地址重写
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void URLRewriter(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
try
{
string rewritePath = string.Empty;
string strHost = app.Request.Url.Host;
string[] arrSeg = app.Request.Url.Segments;
string strHostPre = strHost.Substring(0, strHost.IndexOf(".")).ToLower();
string strPage = arrSeg[arrSeg.Length - 1];
if (arrSeg.Length > 1)
{
rewritePath += getLastSegment(arrSeg);
}
else
{
rewritePath += "/default.aspx";
}
#region 动态参数处理
string strQuery = app.Request.QueryString.ToString();//动态参数处理
if (!string.IsNullOrEmpty(strQuery))
{
if (rewritePath.Contains("?"))
rewritePath += "&" + strQuery;
else
rewritePath += "?" + strQuery;
}
#endregion
app.Context.RewritePath(rewritePath);
}
catch
{
HttpContext.Current.Response.Redirect("http://url.rewrite.com/error/404.htm");
}
}
/// <summary>
/// 处理地址重定向
/// </summary>
/// <param name="arrSeg"></param>
/// <returns></returns>
private string getLastSegment(string[] arrSeg)
{
string reval = string.Empty;
string strPage = arrSeg[arrSeg.Length - 1];//页面,取最后一级
if (strPage.Contains(".htm") || strPage.Contains(".aspx"))
{
for (int i = 0; i < arrSeg.Length - 1; i++)//拼接除页面以外所有节
{
reval += arrSeg[i];
}
string strLastSeg = strPage.Substring(0, strPage.LastIndexOf('.'));//页面去掉扩展名
if (strLastSeg.Contains("-"))//伪静态参数处理
{
string[] subSeg = strLastSeg.Split('-');
reval += subSeg[0] + ".aspx";//加扩展名
for (int j = 1; j < subSeg.Length - 1; j += 2)
{
if (j == 1)
reval += "?" + subSeg[j] + "=" + subSeg[j + 1];
else
reval += "&" + subSeg[j] + "=" + subSeg[j + 1];
}
}
else
{
reval += strLastSeg + ".aspx";//加扩展名
}
}
else
{
for (int i = 0; i < arrSeg.Length; i++)//拼接所有节
{
reval += arrSeg[i];
}
reval += "/default.aspx";
}
return reval;
}
#endregion
/// <summary>
/// 检查用户登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckUserLogin(object sender, EventArgs e)
{
}
public void Dispose() { }
}
}