Sometimes you want to redirect users request to a different one when you want to retire old ones but still provide backward-compatibility. David Wang has a good summary on different redirects for IIS in his blog entry. Here I just want to add a few points which have helped people to resolve tricky problems.

First of all, as David pointed out, there are two (or three in David’s terms) types of redirects:

·         Client-side redirection: the server sends a “302 Redirect” response to the client together with the new location header.

·         Server-side redirection: the server rewrites the request with another URL transparently and the client is not aware of anything.

Client-side redirection

It is also called HTTP redirection. You can achieve it in the following ways.

Redirect in IIS6

You can use the HttpRedirect Metabase setting to perform redirection in IIS6. This property can be set to a virtual application, a file, or a virtual directory (a.k.a. web folder). It requires the physical existence of these “source” items. To achieve that, you can use IIS Manager. Here are the steps on how to set it for a file:

·         Right click on the file and click on Properties

·         Click on “File” tab of the dialog

·         Select “A redirection to a URL” radio box

·         Type in your new Url, and/or make any other changes, and click OK.

After this, you would have setup the file so that all requests to this file is redirected to the new Url. You can check the Metabase setting with the following command:

cscript.exe %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs get /w3svc/1/Root/foo/p1.aspx/HttpRedirect

If the file does not have any Metabase property pre-defined in the Metabase, you cannot use adsutil.vbs to set this property. But you can set it with the script to a virtual application.

Redirect in IIS7

In IIS7, the concept is similar as that of IIS6. You can use the new command “appcmd.exe” to enable redirection as following:

%windir%\system32\inetsrv\appcmd set config "Default Web Site/foo" -section:system.webServer/httpRedirect -enabled:true -destination:"http://localhost/bar/p1.aspx"

This tells IIS to redirect all request sending to the virtual application “/foo” to “/bar/p1.aspx”. The actual result is that appcmd.exe adds the following section to the web.config file for the application “/foo”:

<system.webServer>

    <httpRedirect enabled="true" destination="http://localhost/bar/bbb.aspx" />

</system.webServer>

Redirect with ASP.NET

You can also use ASP.NET to implement client-side redirection. If you don’t want ASP.NET pipeline to intercept any such request, you would need to implement the logic in the global.asax file. This is important for WCF applications since WCF intercepts requests from the pipeline for WCF requests. Here is some sample code for a global.asax file:

<%@ Application Language="C#" Debug="true" %>

 

<script runat="server">

    // This is the application file that handles ASP.NET application-level

    // events. It works for ASP.NET HTTP pipeline. Note that this mechanism

    // does not work for non-HTTP case.

    void Application_BeginRequest(object sender, EventArgs e)

    {

        if (string.Compare(this.Request.Url.AbsolutePath, "/foo/p1.aspx", StringComparison.OrdinalIgnoreCase) == 0)

        {

            this.Response.Redirect("/foo/p2.aspx", true);  

        }

    }

</script>

This approach is more flexible than those provided by IIS:

·         It does not require the existence of the physical file.

·         It is imperative and thus you can add any custom programming logic to determine whether you want to redirect the request. For example, you can check the query string to see whether it contains the information that requires a redirection.

Server-side redirection

It is sometimes also called Url rewriting or Url aliasing. One of the advantages for server-side redirection is performance because there is no round-trip involved if the redirection is on the same machine.

Redirect with ASP.NET

You can use ASP.NET to implement the server-side redirection with Server.Transfer(path) method.