ASP.NET HTTP module for URL redirections
For my own web sites, I needed to perform multiple URL rewritings, both in order to get short URLs and to charm Google and friends. You can see this at work for the SharpToolbox for example.
I improved Fritz Onion's HTTP Module, which performs redirects based on entries in your web.config file. It is regular expression-based, and is an easy way to perform automatic redirection for one set of URLs to another in your site.
I improved performance by keeping Regex instances, and added support for the ~ character.
Here is what the config file can look like:
<redirections type="Madgeek.Web.ConfigRedirections, Madgeek.RedirectModule">
<add ignoreCase="true"
targetUrl="^~/FalseTarget.aspx"
destinationUrl="~/RealTarget.aspx" />
<add permanent="true"
targetUrl="^~/2ndFalseTarget.aspx"
destinationUrl="~/RealTarget.aspx" />
<add
targetUrl="^~/(Author|Category|Tool)([A-Za-z0\d]{8}-?[A-Za-z\d]{4}-?[A-Za-z\d]{4}-?[A-Za-z\d]{4}-?[A-Za-z\d]{12}).aspx$"
destinationUrl="~/Pages/$1.aspx?$1=$2" />
<add
targetUrl="^~/SomeDir/(.*).aspx\??(.*)"
destinationUrl="~/Pages/$1/Default.aspx?$2" />
</redirections>
I joined a small demo to the source code. Download.
Update: 2004/11/19 - added support for ignoring case. See the ignoreCase attribute.
Update: 2005/05/12 - works with .NET 2.0, you just have to replace the calls to ConfigurationSettings.GetConfig() by calls to the new ConfigurationManager.GetSection()