Redirect module

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

原文地址:https://www.cnblogs.com/imxh/p/1432807.html