In the past, Ms urlrewriter was used for URL rewriting. Many shortcomings were found, and the custom functions were too weak. With the addition of rewrite rules, the web. in fact, URL rewriting is the implementation of the ihttphandler interface.
The entire process is divided into two steps:
1. Use an XML file to store rewrite rules. These rules are some simple regular expressions.
2. Implement the ihttphandler Interface
First, let's take a look at the XML file format:
<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<RegEx>
<! -- Rewrite the virtual address -->
<B> <! [CDATA [xxx ,(? <ID> 00000-9000000000000.html $]> </B>
<! -- Actual address -->
<A> <! [CDATA [xxx. aspx? Id =$ {ID}]> </a>
</RegEx>
</Root>
I believe everyone can understand the XML above.
Using system;
Using system. IO;
Using system. Data;
Using system. configuration;
Using system. Collections. Generic;
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;
Using system. text;
Using system. Text. regularexpressions;
Using Microsoft. VisualBasic;
// Regexinfo structure, used to store data read from XML files
Public struct regexinfo
{
Public String _ before;
Public String _ after;
Public regexinfo (string before, string after)
{
_ Before = before. ToLower ();
_ After = after. ToLower ();
}
}
// IpFilter structure, used to store the blocked IP Address
Public struct ipFilter
{
Public string _ ip;
Public ipFilter (string ip)
{
_ Ip = ip;
}
}
Public class HtmlHttpHandler: IHttpHandler // implements the IHttpHandler Interface
{
Private List <RegexInfo> _ regex_list = new List <RegexInfo> ();
Private List <ipFilter> _ ip_filter = new List <ipFilter> ();
Public HtmlHttpHandler ()
{
DataSet ds = new DataSet ();
// Read the url rewriting rule file and write it to the instance in the RegexInfo Structure
Ds. ReadXml (System. Web. HttpContext. Current. Server. MapPath ("~ /App_Data/Regexs. xml "));
Foreach (DataRow r in ds. Tables ["regex"]. Rows)
_ Regex_list.Add (new RegexInfo (string) r ["B"]). Trim (), (string) r ["a"]). Trim ()));
Ds. Reset ();
// Read the list of blocked IP addresses
DS. readxml (system. Web. httpcontext. Current. server. mappath ("~ /App_data/ipfilter. xml "));
Foreach (datarow R in DS. Tables ["ipfilters"]. Rows)
_ Ip_filter.add (New ipfilter (string) R ["ip"]);
}
Public void processrequest (httpcontext context)
{
String _ IP = context. Request. userhostaddress; // obtain the IP address
Foreach (ipfilter R in _ ip_filter)
{
If (_ ip = r. _ ip)
{
Context. Response. Write ("sorry, your IP:" + _ ip + "has been restricted! ");
Context. Response. End ();
}
}
String path = context. Request. Path. ToLower (); // obtain the currently accessed fake URL
Foreach (RegexInfo r in _ regex_list)
Path = Regex. Replace (path, r. _ before, r. _ after); // match the actual URL
Context. Server. Execute (path );
}
// Override the IsReusable property.
Public bool IsReusable
{
Get {return true ;}
}
}
OK. The IHttpHandler interface is implemented. You can rewrite the URL by configuring web. config.
Add the following content to <system. web> </system. web> of web. config:
<Httphandlers>
<Add verb = "*" Path = "*. html" type = "htmlhttphandler"/>
</Httphandlers>
All the files suffixed with .html are handed over to the htmlhttphandler class for processing. Finally, configure IIS.