Redirect requests to your application with the HttpRedirection module

The HttpRedirection module allows you to configure regular expression-based rules that redirect clients from url A to url B using http redirection.

When clients make a request to your server, and the url matches one of the redirection rules you have configured, the server will respond with a redirection response, telling the client to go to your redirected url to complete the request.  It will look something like this (standard redirection response):

HTTP/1.1 302 Found\r\n
Date: Thu, 24 May 2007 21:32:05 GMT\r\n
Location: /redirection/users/mike/\r\n

\r\n
<html><head><title>Object moved</title></head><body>\r\n
<h2>Object moved to <a href="/redirection/users/mike/">here</a>.</h2>\r\n
</body></html>\r\n


The Location response header will indicate the redirected url to which the client will automatically redirect.  Some older browsers that don’t support this behavior (I don’t know of any at this point) will see the hyperlink and the user will be able to manually click it to go to the new url.


Deploy the HttpRedirection module in your application


IIS5
: The module can only redirect requests made to content-types mapped to ASP.NET.
IIS6: Same as IIS5, unless you configure the IIS6 / ASP.NET 2.0 wildcard mapping support (see the
how-to in the LeechGuard post).  Then, this module will be able to provide redirection for all requests.
IIS7: In Integrated mode applications, this module will by default provide redirection for all requests. 

Setting it up is simple:

1.       Download the sample application, and use it
OR

1.       Drop the module assembly into /BIN

2.       Register the <httpRedirection> configuration section in your web.config file, and configure the desired redirection rules as shown in the example app.


Create HttpRedirection rules for your application


The rules can be expressed in the <httpRedirection> configuration section in your web.config file, and allow you to set the following:

matchUrl

The regular expression matching the incoming request urls.

redirectUrl

The url to redirect to, which can contain regular expression references to capture groups from the matchUrl, allowing you to insert parts of the original url into the new url.

urlPart (optional, defaults to PathAndQuery)

The part of the incoming url to match: the entire url, or just the path and querystring, or path only.  Use this to avoid writing regular expressions to match more of the url then you care about.

redirectType (optional, defaults to Temporary)

The type of redirection you want: temporary, permanent, or see other.

requestStage (optional, defaults to BeginRequest)

The stage of the request processing during which you want the redirection to take place.  Can be BeginRequest, PostAuthenticate, or PostAuthorize

The matchUrl and redirectUrl can contain special variables that expand to various request parameters, including:

·         {Protocol} – the protocol scheme of the url

·         {Hostname} – the hostname of the url

·         {Port} – the port of the url

·         {Path} – the absolute path of the url

·         {Query} – the querystring.  Also {Query:Param} for a value of the specific QS parameter.

·         {SV:Param} – the value of a specific server variable (can use this to get any request header)

·         {User} – the name of the authenticated user

·         {Culture} – the culture for this user

·         {AppPath} – the application path for the current application

·         {DirectoryPath} – the directory path segment of the url

·         {FilePath} – the file path segment of the url

(If you are using the “{“ symbol as part of the regular expression in either matchUrl or redirectUrl, escape it by using “{{“)

You can also configure the redirection module to append the original url in the querystring with the appendOriginalUrl parameter on the <httpRedirection> section, and keep track of the redirection depth so redirection loops can be broken by configuring the maxRedirects parameter.

Here are some example rules you can start with:

<!-- Redirect all requests to www.website.com to website.com -->

<add name="Hostname" urlPart="EntireUrl" matchUrl="^{Protocol}://www\.(\w+\.com.+)" redirectUrl="{Protocol}://$1" />

<!-- Redirect to a localized version of the app -->

<add name="LocalizedApplication" urlPart="PathAndQuery" matchUrl="^{AppPath}/content/(.+)" redirectUrl="{AppPath}/content/{Culture}/$1" />

<!-- Redirect all HTTP requests to application to HTTPS -->

<add name="SSLRedirect" urlPart="EntireUrl" matchUrl="^http://(.+)" redirectUrl="https://$1" />

<!-- Redirect to the same url, placing the Referer http header in the querystring -->

<add name="PlaceRefererInQueryString_NoQuery" urlPart="PathAndQuery" matchUrl="^{Path}$" redirectUrl="{Path}?referrer={SV:HTTP_REFERER}" />

<add name="PlaceRefererInQueryString_WithQuery" urlPart="PathAndQuery" matchUrl="^{Path}\?((?!referrer).*)$" redirectUrl="{Path}?referrer={SV:HTTP_REFERER}&amp;$1" />

 

<!-- Permanently redirect all requests to multiple product pages into a single product catalog with querystring -->

<!-- Permanently redirect all requests to multiple product pages into a single product catalog with querystring -->

<add name="ProductCatalog_NoQuery" urlPart="PathAndQuery" matchUrl="^{AppPath}/products/([^/]+).aspx$" redirectUrl="{AppPath}/products/catalog.aspx?productId=$1" redirectType="Permanent" />

<add name="ProductCatalog_WithQuery" urlPart="PathAndQuery" matchUrl="^{AppPath}/products/([^/]+).aspx{Query}$" redirectUrl="{AppPath}/products/catalog.aspx{Query}&amp;productId=$1" redirectType="Permanent" />

The current version is v1.0, released on May 24, 2007.

1.     Download the module and the sample application.

2.     Download the source code (this code is distributed under the Microsoft Permissive License).

Let me know if you are using it, and if you have any questions / suggestions / complaints by leaving comments.

Published 24 May 07 03:06 by Mike Volodarsky
Attachment(s): HttpRedirection_v1_source.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# ServerSide : Redirect clients in your application with HttpRedirection module said on May 24, 2007 3:19 PM:
PingBack from http://mvolo.com/blogs/serverside/archive/2007/05/24/Redirect-clients-in-your-application-with-HttpRedirection-module.aspx
# ServerSide : Redirect clients in your application with HttpRedirection module said on May 24, 2007 3:22 PM:
PingBack from http://mvolo.com/blogs/serverside/archive/2007/05/24/Redirect-clients-in-your-application-with-HttpRedirection-module.aspx
# BTG said on September 26, 2007 7:39 PM:
Sounds complicated for a simple aspnet requirement. If you move webpages and want all your clients to see the redirect upon any request then why not just create a default error in the web.config like: placed in the root directory and all old website content removed it should easily force everyone to the new site as a default error.
# robg said on March 19, 2008 6:58 PM:
Is there a way to preserve Session state with this module so that, for example, a user could transgress across subdomains in the same session?

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below


About Mike Volodarsky

For the past 5 years, I was the core Program Manager for Microsoft ASP.NET 2.0 and IIS 7.0 products. I drove the design and development of the IIS 7.0 web server core, the IIS FastCGI support, the AppCmd command line tool, the ASP.NET Integrated pipeline, and other special projects around server security, performance, and scalability. Now, I am working on my own on cutting edge web server tech on top of the Microsoft IIS platform, and continue blogging about it here.

About me



For the past 5 years, I was the core server Program Manager for the IIS 7.0 and ASP.NET 2.0 products at Microsoft.
Now, I work on advanced web server tech using IIS 7.0, .NET, and Windows Server 2008 and write about it in this blog.

View Michael Volodarsky's profile on LinkedIn

Writings



TechNet Magazine
>Top 10 Performance Improvements in IIS 7.0

MSDN Magazine
>IIS 7.0: Build Web Server Solutions with End-To-End Extensibility
>IIS 7.0: Enhance Your Apps with the Integrated ASP.NET Pipeline
>IIS 7.0: Explore The Web Server For Windows Vista And Beyond
>Design and Deploy Secure Web Apps with ASP.NET 2.0 and IIS 6.0
>Fast, Scalable, and Secure Session State Management for Your Web Applications


Tools and Modules

LeechGuard
IconHandler 2.0
DirectoryListing
HttpRedirection
IIS Auth for Wordpress
iisschema.exe
PortCheck.exe v2.0

Popular Posts

- ASP.NET 2.0 Breaking Changes on IIS 7.0
- Develop IIS7 modules and handlers with .NET
- Troubleshoot IIS7 errors like a pro
- Troubleshooting 503 / "service unavailable" errors
- Troubleshooting "server not found" errors
- Create IIS7 sites, applications, and virtual directories
- Run Ruby on Rails with IIS FastCGI
- VS Debugging of ASP.NET applications on Windows Vista
- Stop hot-linking with IIS and ASP.NET

Tags

Search

Go

This Blog

Archives

Good IIS Blogs

Disclaimer

These postings are provided as is with no warranties, and confer no rights. The views expressed in this blog are entirely my own.

Syndication