Redirect module

Onion Blog

Syndication

I recently had a need to redirect a number of URLs on this site to a new location (those of you subscibed to this blog are undoubtedly aware of this :) - our URLs should now be stable for the forseeable future, btw). After trying a few different techniques, I decided to build an HttpModule that read entries in web.config to perform the redirections. The configuration file entries are regular expression based, so it is easy to take a whole set of urls and map them to a completely new location (which is exactly what i needed to do). I also added the option of making the redirections permanent (301) or temporary (302).

To whet your appetite, here is a sample of what you can add to your web.config file to automatically perform redirections with this module:

<redirections type="Pluralsight.Website.redirections, redirectmodule">
 <add targetUrl="^/(fritz|aaron|keith|mike)/rss\.xml"
  
        
destinationUrl="/blogs/$1/rss.aspx" permanent="true" />
 <add targetUrl="/foo.aspx" destinationUrl="/bar.aspx" permanent="true" />
 <add targetUrl="/quux.aspx" destinationUrl="/bar.aspx" />
</redirections>
 
 
Credit goes to Craig Andera for his nifty XmlSerializer section handler, and to Keith Brown for lending his regular expression expertise. You can download the module complete with a sample config file here.

Enjoy!

update: The link to Craig's XmlSerializer section handler was changed to its new location on our wiki
update2: xml escape character issues fixed


Posted Jul 21 2004, 08:07 AM by fritz-onion
Filed under:

Comments

help.net wrote Redirect module
on 07-28-2004 9:09 AM
Ben Lovell's Blog wrote Simple URL mapping using ASP.NET 2.0
on 08-18-2004 4:15 AM
Fabrice's weblog wrote ASP.NET HTTP module for URL redirections
on 11-18-2004 7:00 AM
Fabrice's weblog wrote ASP.NET HTTP module for URL redirections
on 11-18-2004 7:09 AM
Fabrice wrote re: Redirect module
on 11-18-2004 7:11 AM
I built on your source code to improve performance by keeping Regex instances, and added support for the ~ character.
See http://weblogs.asp.net/fmarguerie/archive/2004/11/18/265719.aspx
Scott Galloway's Personal Blog wrote Realy nice Regular expression based HttpModule URL Rewriter
on 11-19-2004 2:02 AM
Fritz Onion wrote re: Redirect module
on 11-19-2004 4:01 PM
Nice improvements Fabrice, thanks! I'm going to use your new class on our site.
Fabrice wrote re: Redirect module
on 11-24-2004 5:31 AM
Great! Let me know if you have problems with it, or further improve it.
Steven Grant wrote re: Redirect module
on 11-30-2004 11:44 AM
You should have a look at:

http://httpd.apache.org/docs/mod/mod_rewrite.html

which is basically what you are trying to re-implement. You might get some good ideas for improvement there.



Joel's Virtual Desktop wrote Url Rewriting Using Regular Expressions
on 12-01-2004 7:46 PM
Joel Ross wrote Url Rewriting Using Regular Expressions
on 12-01-2004 7:47 PM
dino wrote re: Redirect module
on 01-06-2005 5:39 AM
There is an alternative rewrite mechanism for Windows IIS: an ISAPI filter. The ISAPI approach does not require that you map all content types to ASP.NET.

Here's an example of one:
http://cheeso.members.winisp.net/dl/IonicIsapiRewriter.zip

This filter is something like the Apache module (mod_rewrite) mentioned by a previous poster.

This particular one is implemented in C and compiles with VS2003 or with the (free) VC++2003 toolkit. It uses PCRE (http://www.pcre.org) for the regular expression support.

Nino.Mobile wrote Fixing my CS and .Text URL redirect issues
on 07-06-2005 11:45 AM
As some of you may have noticed, ever since I moved Community Server to the root of nino.net, I have...
deepak vasudevan wrote re: Redirect module
on 08-09-2005 10:37 AM
hi

pls advise me how to configure from web.config to redirect all pages to asp.net class implementing ihttphandler/ihttphandlerfactory or ihttpmodule.

currently, if i type http://myserver/deepak/test/test.aspx (it is trapped,

but http://myserver/deepak/test/test.htm is not trapped by asp.net but rather iis catches it.

But i was observing that http://weblogs.asp.net/ is able to trap all the requests to ASP.NET .TExt.

How?
Fritz Onion wrote re: Redirect module
on 08-15-2005 5:36 AM
The standard way to do this is to map * to aspnet_isapi.dll in the script map entry for your virtual application. This will route all requests through ASP.NET, not just .as*x.
ComputerZen.com - Scott Hanselman wrote Permanent Redirects with HTTP 301
on 10-20-2005 12:55 PM
Léo Magno wrote re: Redirect module
on 02-22-2006 8:47 AM
Hi,

How to make the redirects for directories, for example:
From the directory "Test" to the page "bla.aspx".

From destination www.x.com/test
Redirect to www.x.com/bla.aspx

thanks
Fritz Onion wrote re: Redirect module
on 03-07-2006 5:26 AM
You need to place a default.aspx file in the directory and then just specifically redirect from www.x.com/test/default.aspx to the target.
ASP.NET and Apache mod_perl Developer wrote What about a .htaccess like approach
on 04-29-2006 12:55 AM
On apache I successfully used a simple technique in the past to redirect a large number of pages from an old site to a new site.
I set up a line in a custom .htaccess file for each redirect and specified a 301 Moved Permanent type of redirect.
This was so simple.
Since web.config follows the spirit of .htaccess in many ways I was REALLY hoping there was an equivalent technique perhaps using Location specifications.
I'm starting to think there might not be.
Even though I love ASP.NET, Apache and mod_perl are still hard to beat in a lot of areas.
Chris Love's Official Blog - Professional ASP.NET wrote URLMapping or URL Rewriting
on 11-24-2006 7:56 AM
URLMapping or URL Rewriting as is is sometimes called, is very important to make your site people and search engine friendly. Well ASP.NET 2.0 is setup to URLMAP and here is some info on URLMApping with Regular Expressions.
Chris wrote re: Redirect module
on 03-08-2007 1:56 PM
I'm transferring from the old bloaty .html file system to the new svelte .aspx file system. Once the DNS transfers the ip's I want to magically be transported to my .aspx files.

Currently, I can get it to work only with .aspx files. How can I get this to module to recognize .html files?

Any help would be greatly appreciated.
I can be reached at the contact url above.

Cheers,

Chris
il! wrote re: Redirect module
on 06-03-2007 9:53 PM
I prefer the approach when everything is done in one query without redirects. The way is in right switch from initial and working Url with the help of HttpContext.RewritePath (“…”) several times. Method RewritePath is used in different events, rewriting Url, in order to receive the right value of <form action=….”> and different modules were correctly realized.

I saw the idea here http://www.urlrewriting.net/en/Default.aspx and made it true.
Sometimes it’s necessary to displace or add HttpContext.RewritePath, if there are special configuration or modules.

Some words about ModRewrite http://httpd.apache.org/docs/mod/mod_rewrite.html
There is no need to look for compromises solutions for old technologies. In ASP.NET phases of query through events are available to a developer. It’s especially good in IIS 7. After this ModRewrite looks a very poor alternative.
And it’s not a pleasure at all to tune operation of ModRewrite through logo.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?