Redirect clients in your application with HttpRedirection module

In a web application, it’s often necessary to redirect clients requesting one url to another url.  Here are some reasons why your application may need to do it:

·         A part of your site should only be accessed over HTTPS to protect private data, so you want to redirect requests made over HTTP to same urls with HTTPS.

·         You want to make sure that everyone accesses your blog as domain.com, and not www.domain.com for better search engine rankings.

·         The structure of your application changed – your product pages used to be in the “/catalog” subdirectory but now they are in the “/products” subdirectory.

In these and many more cases, you will need to redirect clients making requests to some urls in your site that match a particular rule to other urls.  In general, there are server different ways that you can do redirection on a web server:

1.       Http Redirection.  The web server tells the client that the content the client is looking for is located at a different address, and the client automatically (most of the time) requests the redirected address.

2.       Server-side rewriting.  The web server changes the url being requested internally, so that the new resource is processed and returned in response to the original url, without telling the client.

The main difference being that with redirection, the client is aware of the change in url and makes a separate request to the new url, and with rewriting the client is oblivious to the change.

This makes redirection more suitable for when you want the client to see the new url, and possibly remember/bookmark it.  Redirection is also the only option whenever the redirected url does not reside in your site or even on your server (there is a third option, called forwarding / proxying, that allows rewrites to other servers to be done internally – more on that in a later post).

Server-side rewriting is better if the rewrite is for internal purposes only – for example, you expose SEF (search engine friendly) urls to your clients, but internally you need to rewrite to an ASP.NET or PHP script to provide the processing for the request.  Server-side rewriting is often also faster because there is no need to tell the client about the rewrite, and then have the client make a second request.  Rewriting can also be trickier on the server, because it’s a lot more sensitive to when the rewrite is made during the processing of the request.   If it’s not made at the right time, certain processing stages have already took place for the old url, and you may see undesired results.


Doing redirection with the HttpRedirection module


A while ago, I wrote a module to do basic http redirection for an ASP.NET application.  The HttpRedirection module allows you to configure regular expression-based rules that redirect clients from url A to url B using http redirection.  You can also inject useful variables into the match and redirection url expressions, control the type of redirects that are performed (temporary vs. permanent), and in what stage of request processing they are performed.

Here are some examples of the things you can do:

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

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

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

<add name="LocalizedApplication" urlPart="PathAndQuery" matchUrl="^{AppPath}/content/(.+)" redirectUrl="{AppPath}/content/{Culture}/$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" />


I was looking for an image that I could use for this post, and found this (apparently my LeechGuard module is not being used to prevent hot-linking on that website :)) and found this - I think it says it all:

You are being redirected with HttpRedirection module :)

Read more about the module, download it and the associated source code here.

The module is supported both for legacy ASP.NET applications on IIS5+, but can also be used to do redirection for all resource types using IIS6 / ASP.NET 2.0 wildcard mapping support, or the IIS7 integrated pipeline.

That’s it.  Let me know if you are using it, and if you have any questions / suggestions / complaints by leaving comments.   I’ll be covering server-side rewriting and its complexities in a future post.

Thanks,

Mike

Published 24 May 07 02:58 by Mike Volodarsky
Attachment(s): HttpRedirection_v1_sampleapp.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 requests to your application with the HttpRedirection module said on May 24, 2007 3:25 PM:
PingBack from http://mvolo.com/blogs/serverside/pages/Redirect-requests-to-your-application-with-the-HttpRedirection-module.aspx
# Mike Volodarsky's WebLog said on May 24, 2007 3:27 PM:

In a web application, it’s often necessary to redirect clients requesting one url to another url. A while

# iis said on May 24, 2007 4:00 PM:

In a web application, it’s often necessary to redirect clients requesting one url to another url. A while

# Rajiv Das said on May 24, 2007 10:40 PM:
http://localhost:49887/wwwroot/page.aspx?referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer=&referrer= Tried this on firefox?
# Mike Volodarsky said on May 25, 2007 9:01 AM:

Rajiv, Nice catch!

The regular expression in the referrer example was a bit off, causing a loop (regardless of browser).

Unless you are a regular expression master, this is pretty common - a good way to detect and break these loops is to set the "appendOriginalUrl" to true, which causes the redirect to keep track of the number of redirects and break the loops when they exceed the "maxRedirects" number - 3 by default. Very useful when testing.

I fixed the regular expression and updated the sample app.

Thanks!

Mike

# John Mandia's Points of Interest said on May 26, 2007 1:25 PM:

Well this week was a nice rest, most of it spent relaxing with my wife. So it was a non-coding week but

# Johny said on May 30, 2007 2:29 AM:
this is just for ASP.NET right? I'm just thinking if IIS 7 will have something like APACHE's got "mod_rewrite". Now on IIS 6 I need ISAPI_REWRITE modul for PHP. It would be nice to have something like this integrated.
# anony said on June 5, 2007 4:09 PM:
Johny, there is a similar isapi filter to mod_rewrite although it doesn't support all the same features... Take a look at this one: http://www.codeplex.com/IIRF
# Mike Schinkel said on June 5, 2007 4:35 PM:
So I'm trying to get ASP working under IIS7 and want to eliminate ISAPI Rewrite for server-side rewriting. How to do this for ASP on IIS7?
# Mike Volodarsky said on June 8, 2007 10:10 AM:

Mike,

You can write a simple ASP.NET module that runs for all requests, and does the rewriting you want.  Then you can place this module in your application, so there is no need to install anything on the server ...

I'll cover rewriting in IIS7 in a future post in more detail ...

Thanks,

Mike

# Luke Hatcher said on July 17, 2007 7:36 AM:
Mind my IIS ignorance, but I can't figure out how to actually use this. When you say use the sample app, where do I add that to an existing web site being served with IIS?
# Halil said on August 15, 2007 10:36 PM:
<< I'll cover rewriting in IIS7 in a future post in more detail >> Well, Im looking forward to that. Like as Johny said; Im just wondering if IIS7 will have something like Apache's mod_rewrite feature?
# Mike Volodarsky's ServerSide said on October 28, 2007 3:13 AM:

During IIS7 development, I&#39;ve written quite a few modules for IIS7 / ASP.NET. Some of these were

# cuff@interware.net said on November 15, 2007 6:38 PM:
I want a user to login to my C++ application (which autheticates him) Then he can click a button and be redirected to another remote site. Can I do an invisble login for the user into the remote site (windows standard authentication) - I do not want the user to know the username/password for the login. I noticed in the HttpRedirection above has a field for Postauthenticate.
# Mike Volodarsky said on December 8, 2007 1:24 PM:

Cuff,

PostAuthenticate allows the user to be redirected only after he/she succesfully authenticates against the original URL.  Depending on the authentication scheme, the browser may keep the authentication result in reuse it for the redirected URL.  Windows Authentication will stay in effect if the browser makes the subsequent request on the same connection.  So, if the url is in the same site, this should work.  Basic authentication should stay if the new URL is under the old URL.  Forms Authentication will stay if the URL is in the same ASP.NET application.

If the new URL is on another site, or server, most likely you will need to re-authenticate.  Another option you have is implementing a single-signon scheme where your original site produces an authentication token and injects it in the redirected URL, and the second site can authenticate the user based on that token.

Hopefully this answers your question.

Thanks,

Mike

# Bob said on December 18, 2007 2:31 AM:
I don't know why, but in IIS7 when I'm using rewriting the RawUrl from HttpContext... return original url not rewritten url. In II6, IIS5 it returns rewritten url.
# Alan Moberg said on August 20, 2008 10:00 PM:
I have had my URL to my e-mail account at Juno.com redirected to another site. That site is blocked by my spyware provider. Result I cannot get to my e-mail account. Where would I look for that redirection program? How do I get rid of it? I think I have removed the carrier that brought the function in, antivirus2008XP, but I still cannot be certain I got all of it. Thanks,
# Mike Volodarsky said on August 21, 2008 5:05 PM:

Hi Alan,

Your question is out of topic here, you'll have better luck trying elsewhere.

Thanks,

Mike

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