http://www.codeproject.com/KB/aspnet/urlrewriter.aspx
Web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="urlrewrites" type="ThunderMain.URLRewriter.Rewriter, ThunderMain.URLRewriter, Version=1.0.783.30976, Culture=neutral, PublicKeyToken=7a95f6f4820c8dc3"/>
</sectionGroup>
</configSections>
<system.web>
<!--
Maps from old website to new website using Regular Expressions
rule/url - old website url (Regular Expression)
rule/rewrite - new website replacement expression
Of two or more rules which match a given request, the first
will always take precedance.
Created : 2002-02-22 : Richard Birkby
Proposed extra features:
Cache a number of URL Rewrites
Allow the rule to determine whether to rewrite the URL, or to redirect the client
-->
<urlrewrites>
<rule>
<url>/urlrewriter/show/.asp</url>
<rewrite>show.aspx</rewrite>
</rule>
<rule>
<url>/urlrewriter/wohs/.asp</url>
<rewrite>show.aspx</rewrite>
</rule>
<rule>
<url>/urlrewriter/show(.*)/.asp</url>
<rewrite>show.aspx?$1</rewrite>
</rule>
<rule>
<url>/urlrewriter/(.*)show/.html</url>
<rewrite>show.aspx?id=$1&cat=2</rewrite>
</rule>
<rule>
<url>/urlrewriter/s/h/o/w/(.*)/.html</url>
<rewrite>/urlrewriter/show.aspx?id=$1</rewrite>
</rule>
</urlrewrites>
<!-- DYNAMIC DEBUG COMPILATION
Set compilation debug="true" to enable ASPX debugging. Otherwise, setting this value to
false will improve runtime performance of this application.
Set compilation debug="true" to insert debugging symbols (.pdb information)
into the compiled page. Because this creates a larger file that executes
more slowly, you should set this value to true only when debugging and to
false at all other times. For more information, refer to the documentation about
debugging ASP .NET files.
-->
<compilation
defaultLanguage="c#"
debug="true"
/>
<!-- CUSTOM ERROR MESSAGES
Set customError mode values to control the display of user-friendly
error messages to users instead of error details (including a stack trace):
"On" Always display custom (friendly) messages
"Off" Always display detailed ASP.NET error information.
"RemoteOnly" Display custom (friendly) messages only to users not running
on the local Web server. This setting is recommended for security purposes, so
that you do not display application detail information to remote clients.
-->
<customErrors
mode="RemoteOnly"
/>
<!-- AUTHENTICATION
This section sets the authentication policies of the application. Possible modes are "Windows", "Forms",
"Passport" and "None"
-->
<authentication mode="Windows" />
<!-- APPLICATION-LEVEL TRACE LOGGING
Application-level tracing enables trace log output for every page within an application.
Set trace enabled="true" to enable application trace logging. If pageOutput="true", the
trace information will be displayed at the bottom of each page. Otherwise, you can view the
application trace log by browsing the "trace.axd" page from your web application
root.
-->
<trace
enabled="false"
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>
<!-- SESSION STATE SETTINGS
By default ASP .NET uses cookies to identify which requests belong to a particular session.
If cookies are not available, a session can be tracked by adding a session identifier to the URL.
To disable cookies, set sessionState cookieless="true".
-->
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;user id=sa;password="
cookieless="false"
timeout="20"
/>
<!-- GLOBALIZATION
This section sets the globalization settings of the application.
-->
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>
</system.web>
</configuration>
============
Show.aspx
<%@ Page language="c#"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Show</title>
</head>
<body>
<script runat="server">
static int x=1;
private void Page_Load(object sender, System.EventArgs e) {
Response.Write("Page= <b>" + Request.Url + "</b><br> called " + x + " times" );
x++;
}
</script>
</body>
</html>
===========
Rewriter.cs
using System;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using System.Configuration;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Xml.Xsl;
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile(@"keyfile.snk")]
[assembly: AssemblyKeyName("")]
[assembly: AssemblyVersion("1.0.783.30976")]
namespace ThunderMain.URLRewriter {
public class Rewriter : IConfigurationSectionHandler {
protected XmlNode _oRules=null;
protected Rewriter(){}
public string GetSubstitution(string zPath) {
Regex oReg;
foreach(XmlNode oNode in _oRules.SelectNodes("rule")) {
oReg=new Regex(oNode.SelectSingleNode("url/text()").Value);
Match oMatch=oReg.Match(zPath);
if(oMatch.Success) {
return oReg.Replace(zPath,oNode.SelectSingleNode("rewrite/text()").Value);
}
}
return zPath;
}
public static void Process() {
Rewriter oRewriter=(Rewriter)ConfigurationSettings.GetConfig("system.web/urlrewrites");
string zSubst=oRewriter.GetSubstitution(HttpContext.Current.Request.Path);
if(zSubst.Length>0) {
HttpContext.Current.RewritePath(zSubst);
}
}
#region Implementation of IConfigurationSectionHandler
public object Create(object parent, object configContext, XmlNode section) {
_oRules=section;
// TODO: Compile all Regular Expressions
return this;
}
#endregion
}
}
===========
Make Rewriter.bat
@Echo Off
csc /target:library /out:bin/ThunderMain.URLRewriter.dll rewriter.cs
Echo Compilation Complete
Echo.
Echo Now install into the GAC by dragging and dropping bin/ThunderMain.URLRewriter.dll into C:/Windows/assembly
Echo.
pause
============
default.aspx
<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>default</title>
</HEAD>
<body>
<form id="default" method="post" runat="server">
<UL>
<LI>
<A href="show.asp">show.asp</A>
<LI>
<A href="wohs.asp">wohs.asp</A>
<LI>
<A href="show5.asp">show5.asp</A>
<LI>
<A href="42show.html">42show.html</A>
<LI>
<A href="s/h/o/w/5.html">s/h/o/w/5.html</A></LI></UL>
<P> </P>
</form>
</body>
</HTML>
===================
Global.asax
<%@Assembly Name="ThunderMain.URLRewriter" %>
<script language="C#" runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e){
ThunderMain.URLRewriter.Rewriter.Process();
}
</script>
Web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="urlrewrites" type="ThunderMain.URLRewriter.Rewriter, ThunderMain.URLRewriter, Version=1.0.783.30976, Culture=neutral, PublicKeyToken=7a95f6f4820c8dc3"/>
</sectionGroup>
</configSections>
<system.web>
<!--
Maps from old website to new website using Regular Expressions
rule/url - old website url (Regular Expression)
rule/rewrite - new website replacement expression
Of two or more rules which match a given request, the first
will always take precedance.
Created : 2002-02-22 : Richard Birkby
Proposed extra features:
Cache a number of URL Rewrites
Allow the rule to determine whether to rewrite the URL, or to redirect the client
-->
<urlrewrites>
<rule>
<url>/urlrewriter/show/.asp</url>
<rewrite>show.aspx</rewrite>
</rule>
<rule>
<url>/urlrewriter/wohs/.asp</url>
<rewrite>show.aspx</rewrite>
</rule>
<rule>
<url>/urlrewriter/show(.*)/.asp</url>
<rewrite>show.aspx?$1</rewrite>
</rule>
<rule>
<url>/urlrewriter/(.*)show/.html</url>
<rewrite>show.aspx?id=$1&cat=2</rewrite>
</rule>
<rule>
<url>/urlrewriter/s/h/o/w/(.*)/.html</url>
<rewrite>/urlrewriter/show.aspx?id=$1</rewrite>
</rule>
</urlrewrites>
<!-- DYNAMIC DEBUG COMPILATION
Set compilation debug="true" to enable ASPX debugging. Otherwise, setting this value to
false will improve runtime performance of this application.
Set compilation debug="true" to insert debugging symbols (.pdb information)
into the compiled page. Because this creates a larger file that executes
more slowly, you should set this value to true only when debugging and to
false at all other times. For more information, refer to the documentation about
debugging ASP .NET files.
-->
<compilation
defaultLanguage="c#"
debug="true"
/>
<!-- CUSTOM ERROR MESSAGES
Set customError mode values to control the display of user-friendly
error messages to users instead of error details (including a stack trace):
"On" Always display custom (friendly) messages
"Off" Always display detailed ASP.NET error information.
"RemoteOnly" Display custom (friendly) messages only to users not running
on the local Web server. This setting is recommended for security purposes, so
that you do not display application detail information to remote clients.
-->
<customErrors
mode="RemoteOnly"
/>
<!-- AUTHENTICATION
This section sets the authentication policies of the application. Possible modes are "Windows", "Forms",
"Passport" and "None"
-->
<authentication mode="Windows" />
<!-- APPLICATION-LEVEL TRACE LOGGING
Application-level tracing enables trace log output for every page within an application.
Set trace enabled="true" to enable application trace logging. If pageOutput="true", the
trace information will be displayed at the bottom of each page. Otherwise, you can view the
application trace log by browsing the "trace.axd" page from your web application
root.
-->
<trace
enabled="false"
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>
<!-- SESSION STATE SETTINGS
By default ASP .NET uses cookies to identify which requests belong to a particular session.
If cookies are not available, a session can be tracked by adding a session identifier to the URL.
To disable cookies, set sessionState cookieless="true".
-->
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;user id=sa;password="
cookieless="false"
timeout="20"
/>
<!-- GLOBALIZATION
This section sets the globalization settings of the application.
-->
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>
</system.web>
</configuration>
============
Show.aspx
<%@ Page language="c#"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Show</title>
</head>
<body>
<script runat="server">
static int x=1;
private void Page_Load(object sender, System.EventArgs e) {
Response.Write("Page= <b>" + Request.Url + "</b><br> called " + x + " times" );
x++;
}
</script>
</body>
</html>
===========
Rewriter.cs
using System;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using System.Configuration;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Xml.Xsl;
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile(@"keyfile.snk")]
[assembly: AssemblyKeyName("")]
[assembly: AssemblyVersion("1.0.783.30976")]
namespace ThunderMain.URLRewriter {
public class Rewriter : IConfigurationSectionHandler {
protected XmlNode _oRules=null;
protected Rewriter(){}
public string GetSubstitution(string zPath) {
Regex oReg;
foreach(XmlNode oNode in _oRules.SelectNodes("rule")) {
oReg=new Regex(oNode.SelectSingleNode("url/text()").Value);
Match oMatch=oReg.Match(zPath);
if(oMatch.Success) {
return oReg.Replace(zPath,oNode.SelectSingleNode("rewrite/text()").Value);
}
}
return zPath;
}
public static void Process() {
Rewriter oRewriter=(Rewriter)ConfigurationSettings.GetConfig("system.web/urlrewrites");
string zSubst=oRewriter.GetSubstitution(HttpContext.Current.Request.Path);
if(zSubst.Length>0) {
HttpContext.Current.RewritePath(zSubst);
}
}
#region Implementation of IConfigurationSectionHandler
public object Create(object parent, object configContext, XmlNode section) {
_oRules=section;
// TODO: Compile all Regular Expressions
return this;
}
#endregion
}
}
===========
Make Rewriter.bat
@Echo Off
csc /target:library /out:bin/ThunderMain.URLRewriter.dll rewriter.cs
Echo Compilation Complete
Echo.
Echo Now install into the GAC by dragging and dropping bin/ThunderMain.URLRewriter.dll into C:/Windows/assembly
Echo.
pause
============
default.aspx
<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>default</title>
</HEAD>
<body>
<form id="default" method="post" runat="server">
<UL>
<LI>
<A href="show.asp">show.asp</A>
<LI>
<A href="wohs.asp">wohs.asp</A>
<LI>
<A href="show5.asp">show5.asp</A>
<LI>
<A href="42show.html">42show.html</A>
<LI>
<A href="s/h/o/w/5.html">s/h/o/w/5.html</A></LI></UL>
<P> </P>
</form>
</body>
</HTML>
===================
Global.asax
<%@Assembly Name="ThunderMain.URLRewriter" %>
<script language="C#" runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e){
ThunderMain.URLRewriter.Rewriter.Process();
}
</script>