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}&$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}&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.
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.