<script RunAt="server" Language="C#"> void Application_BeginRequest() { WebSite.UrlRewrite(new[] { new[]{ @"Ware\[(\d+)\]\.html$", "Detail.aspx?ID=$1" }, new[]{ @"News\[(\d+)\]\.html$", "XinWen.aspx?ID=$1" } }); } </script>
using System.Text.RegularExpressions; public class WebSite { public static void UrlRewrite(string[][] arrRules) { var Context = System.Web.HttpContext.Current; var strUrl = Context.Request.Path; foreach(var rule in arrRules) { strUrl = Regex.Replace(strUrl, rule[0], rule[1], RegexOptions.IgnoreCase); } // 如未发现任何更改,不进行URL重写。 if (strUrl == Context.Request.Path) return; // 附加重写前的相关URL参数 var strPar = Context.Request.QueryString + ""; if (strPar != "") strUrl += "&" + strPar; Context.RewritePath(strUrl); } }
import System; import System.Web; package Rimifon { class UrlRewrite implements IHttpModule { static var cfg = [ [ /^\/Portal\//i, "/home/?p=/" ] ]; function Init(ha : HttpApplication) { ha.add_BeginRequest(doUrlRewrite); } function doUrlRewrite(obj, ea : EventArgs) { var ctx : HttpContext = obj.Context; var qstr = ctx.Request.ServerVariables["QUERY_STRING"]; var path = ctx.Request.Path; if(!!qstr) qstr = "&" + qstr; function testUrl(reg, url) { if(!reg.test(path)) return false; ctx.RewritePath(path.replace(reg, url) + qstr); return true; } for(var i = 0; i < cfg.length; i++) { if(testUrl.apply(cfg, cfg[i])) return; } } function Dispose() { } } }