1: The problem of deletion during rewriting in asp.net1.1! ! !
Such as the following regular expression:
<Rules>
<RewriterRule>
<LookFors>
<LookFor>~/(d{4})/(d{2}).html</LookFor>---------<1>
<LookFor>~/(d{4})/(d{2})/</LookFor>--------------<2>
<LookFor>~/(d{4})/(d{2})</LookFor>-----------<3>
<LookFor>~/(d{4})/(d{2})/index.html</LookFor>----<4>
</LookFors>
<SendTo>~/Pro.aspx?year=$1&month=$2</SendTo>
</RewriterRule>
</Rules>
Among them, 1 and 4 can be mapped to the corresponding page normally
, but 2 and 3 will cause an http404 error! ! !
The reason lies in the processing flow of IIS itself. The solution is to rewrite the 404 error processing on the website itself! ! !
1: Customize the URL for handling 404 errors (configured in IIS, the configuration in web.config is useless for rewriting)
2: Add the following section to the System.Web section:
<httpHandlers>
<add verb="*" path="404.aspx" type="lt.Http404,lt"></add>
</httpHandlers>
<httpModules>
<add type="lt.ReWriteModule,lt" name="ModuleRewriter" />
</httpModules>
The source code is as follows:
public class Http404:System.Web.IHttpHandler
{
public Http404()
{
//
// TODO: add constructor logic here
//
}
#region IHttpHandler member
public void ProcessRequest(System.Web.HttpContext context)
{
// TODO: Add Http404.ProcessRequest implementation
string errorPath=context.Request.RawUrl.Split(new char[]{';'})[1];
string appPath=context.Request.ApplicationPath;
int ipos=errorPath.IndexOf(appPath);
string url=errorPath.Substring(ipos+appPath.Length );
// if(!url.EndsWith("/"))
// {
// url+="/";
// }
// url+="index.html";
// context.Response.Write(url);
// context.RewritePath(url);
//context.Response.Write(url);
url="~"+url;
string newUrl =lt.ReWriteModule.GetUrl(context,url);
//context.Response.Write(newUrl);
if (newUrl != null)
{
//cxt.Response.Filter = new ResponseFilter(cxt.Response.Filter,cxt.Request.Path);
context.Response.Write("Requested path:" + url);
context.Response.Write("<BR>");
context.Response.Write("Redirected destination URL: " + newUrl);
context.Response.Write("<BR>");
context.RewritePath(newUrl);
}
else
{
context.Response.Write("The resource you requested does not exist!!");
context.Response.End ();
}
}
public bool IsReusable
{
get
{
// TODO: Add Http404.IsReusable getter implementation
return false;
}
}
/////////////////The httpModule in configuration section processing is as follows:
public class ReWriteModule:System.Web.IHttpModule
{
public ReWriteModule()
{
//
// TODO: add constructor logic here
//
}
#region IHttpModule member
public void Init(System.Web.HttpApplication context)
{
// TODO: Add ReWriteModule.Init implementation
context.BeginRequest+=new EventHandler(this.ReWrite);
}
private static System.Xml.XmlDocument ruleDoc = null;
private static System.Xml.XmlDocument GetRuleConfig(System.Web.HttpContext app)
{
if (ruleDoc == null)
{
ruleDoc = new System.Xml.XmlDocument();
ruleDoc.Load(app.Server.MapPath("~/rule.xml"));
}
return ruleDoc;
}
public static string GetUrl(System.Web.HttpContext cxt,string path)
{
System.Xml.XmlDocument doc = GetRuleConfig(cxt);
System.Xml.XmlNodeList lst= doc.GetElementsByTagName("RewriterRule");
string pat="";
foreach (System.Xml.XmlNode nd in lst)
{
System.Xml.XmlNodeList sub = nd.ChildNodes[0].ChildNodes;
foreach(System.Xml.XmlNode chk in sub)
{
pat = "^" + chk.InnerText+"$";
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pat, System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if(reg.IsMatch(path))
{
return reg.Replace(path, nd.ChildNodes[1].InnerText);
}
}
}
return null;
}
private void ReWrite(object sender,EventArgs e)
{
System.Web.HttpContext cxt =(sender as System.Web.HttpApplication).Context;
if (cxt.Request.ContentType != "image/pjpeg")
{
string type = cxt.Request.ContentType.ToLower();
string path = cxt.Request.Path;
string apppath = cxt.Request.ApplicationPath;
path = path.Remove(0, apppath.Length);
path = "~" + path;
string newUrl = GetUrl(cxt, path.TrimEnd().TrimStart());
if (newUrl != null)
{
//cxt.Response.Filter = new ResponseFilter(cxt.Response.Filter,cxt.Request.Path);
cxt.Response.Write("Requested path:" + path);
cxt.Response.Write("<BR>");
cxt.Response.Write("Redirected destination URL: " + newUrl);
cxt.Response.Write("<BR>");
cxt.RewritePath(newUrl);
}
//else
//{
// cxt.Response.Write(cxt.Request.Path + "<BR>");
// cxt.Response.Write("The resource you requested does not exist or you have no permission to access!");
// cxt.Response.Flush();
// cxt.Response.End();
//}
}
}
public void Dispose()
{
// TODO: Add ReWriteModule.Dispose implementation
}
#endregion
}
---------rule.xml is configured as follows:
<?xml version="1.0" encoding="utf-8" ?>
<Rules>
<RewriterRule>
<LookFors>
<LookFor>~/(d{4})/(d{2}).html</LookFor>
<LookFor>~/(d{4})/(d{2})/</LookFor>
<LookFor>~/(d{4})/(d{2})</LookFor>
<LookFor>~/(d{4})/(d{2})/index.html</LookFor>
</LookFors>
<SendTo>~/Pro.aspx?year=$1&month=$2</SendTo>
</RewriterRule>
<RewriterRule>
<LookFors>
<LookFor>~/Pro.aspx?year=(d{4})&month=(d{2})</LookFor>
</LookFors>
<SendTo>~/(d{4})/(d{2}).html</SendTo>
</RewriterRule>
<RewriterRule>
<LookFors>
<LookFor>~/pc</LookFor>
</LookFors>
<SendTo>~/Test2.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFors>
<LookFor>~/index.html</LookFor>
<LookFor>~/default.html</LookFor>
</LookFors>
<SendTo>~/default.aspx</SendTo>
</RewriterRule>
</Rules>
/////////For action changes caused by rewriting, please refer to the issue of urlMappings in asp.net2.0 written by me! ! ! ! !