【概述】html
URL重寫就是首先得到一個進入的URL請求而後把它從新寫成網站能夠處理的另外一個URL的過程。重寫URL是很是有用的一個功能,由於它可讓你提升搜索引擎閱讀和索引你的網站的能力;並且在你改變了本身的網站結構後,無須要求用戶修改他們的書籤,無需其餘網站修改它們的友情連接;它還能夠提升你的網站的安全性;並且一般會讓你的網站更加便於使用和更專業。web
1 class MyUrlWriter : IHttpModule 2 { 3 public void Init( HttpApplication context) 4 { 5 context.BeginRequest += new EventHandler (context_BeginRequest); 6 } 7 8 protected void context_BeginRequest( object sender, EventArgs e) 9 { 10 HttpApplication application = sender as HttpApplication ; 11 HttpContext context = application.Context; //上下文 12 string url = context.Request.Url.LocalPath; //得到請求URL 13 14 Regex articleRegex = new Regex ("/Article/[A-Z0-9a-z_]+" ); //定義規則 15 if (articleRegex.IsMatch(url)) 16 { 17 string paramStr = url.Substring(url.LastIndexOf('/' ) + 1); 18 context.RewritePath( "/Article.aspx?id=" + paramStr); 19 } 20 else 21 { 22 context.RewritePath( "/Default.aspx" ); 23 } 24 } 25 26 public void Dispose() { } 27 }
1 <httpModules > 2 <add name = "UrlReWriter " type =" UrlReWriter.MyUrlWriter,UrlReWriter " /> 3 </httpModules >
1 public partial class Article : System.Web.UI.Page 2 { 3 protected void Page_Load( object sender, EventArgs e) 4 { 5 Response.Write(Request.QueryString[ "id" ]); 6 } 7 }
1 <a href ="/Article/35">測試url重寫</a>
1 /// <summary> 2 /// Provides a rewriting HttpModule. 3 /// </summary> 4 public class ModuleRewriter : BaseModuleRewriter 5 { 6 /// <summary> 7 /// This method is called during the module's BeginRequest event. 8 /// </summary> 9 /// <param name="requestedRawUrl"> The RawUrl being requested (includes path and querystring).</param> 10 /// <param name="app"> The HttpApplication instance. </param> 11 protected override void Rewrite( string requestedPath, System.Web.HttpApplication app) 12 { 13 // log information to the Trace object. 14 app.Context.Trace.Write( "ModuleRewriter" , "Entering ModuleRewriter"); 15 16 // get the configuration rules 17 RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules; 18 19 // iterate through each rule... 20 for (int i = 0; i < rules.Count; i++) 21 { 22 // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) 23 string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$" ; 24 25 // Create a regex (note that IgnoreCase is set...) 26 Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); 27 28 // See if a match is found 29 if (re.IsMatch(requestedPath)) 30 { 31 // match found - do any replacement needed 32 string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath,re.Replace(requestedPath, rules[i].SendTo)); 33 34 // log rewriting information to the Trace object 35 app.Context.Trace.Write( "ModuleRewriter" , "Rewriting URL to " + sendToUrl); 36 37 // Rewrite the URL 38 RewriterUtils.RewriteUrl(app.Context, sendToUrl); 39 break ; // exit the for loop 40 } 41 } 42 43 // Log information to the Trace object 44 app.Context.Trace.Write( "ModuleRewriter" , "Exiting ModuleRewriter"); 45 } 46 }
1 public partial class Article : System.Web.UI.Page 2 { 3 protected void Page_Load( object sender, EventArgs e) 4 { 5 Response.Write(Request.QueryString[ "id" ]); 6 } 7 }
public partial class News : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { Response.Write( string .Format("日期:{0}<br/>" , Request.QueryString["date" ])); Response.Write( string .Format("ID:{0}<br/>" , Request.QueryString["id" ])); } }
1 <configuration> 2 <configSections> 3 <section name = "RewriterConfig " type =" URLRewriter.Config.RewriterConfigSerializerSectionHandler,URLRewriter " /> 4 </configSections> 5 </configuration>
1 <httpHandlers> 2 <remove verb = "* " path = "*.asmx " /> 3 <add verb = "* " path = "* " type = "URLRewriter.RewriterFactoryHandler, URLRewriter" /> 4 <add verb = "* " path = "*.html " type =" URLRewriter.RewriterFactoryHandler, URLRewriter " /> 5 </httpHandlers>
1 <configuration> 2 <RewriterConfig> 3 <Rules> 4 <RewriterRule> 5 <LookFor>~/Article/([A-Z0-9a-z_]+) </LookFor> 6 <SendTo>~/Article.aspx?id=$1 </SendTo> 7 < RewriterRule> 8 <RewriterRule> 9 <LookFor>~/News/(\d{4}-\d{2}-\d{2})/(\d{1,6})\.html? </LookFor> 10 <SendTo>~/News.aspx?date=$1 & id=$2</SendTo> 11 </RewriterRule> 12 </Rules> 13 </RewriterConfig> 14 </configuration>
1 <a href ="<% = ResolveUrl("Article/35")%> "> 測試文章url重寫</a>< br /> 2 <a href ="<% = ResolveUrl("News/2014-06-11/285864.html")%> "> 測試新聞url重寫</a>