Redirecting from http to https in IIS7


I was thinking to write an HttpModule for IIS7 and wanted a simple, useful and easily understandable scenario. Working with IIS customers for last 3 years one of those common scenario came into my mind, Redirecting http traffic to https. Although this is pretty straight forward requirement, till IIS6 it was difficult to achieve. Check the following KB 839357 (specifically for OWA scenario) which explains the cumbersome steps .

(If you are looking for same functionality in prior versions of IIS, find it at the bottom of this post)

HttpModules are not something new if you are an ASP.NET developer. But there are some differences. In IIS7 because of integrated pipeline, HttpModule applies to any request not just ASP.NET and that includes htm,jpg,asp,aspx,asmx,php,jsp...

So let us get into action (I'm using C# for this sample)

  1. Download and Install IIS7 Managed Module Starter Kit
    (Not really a requirement but it would make developing IIS7 modules easier)
  2. Rename the default class name created to "redir.cs" and rename project/solution/namespace to "http2https"
  3. Add the following code in "Init" method

    // register for the BeginRequest event
    application.BeginRequest += new EventHandler(OnBeginRequest);

  4. Add the following method to implement "BeginRequest" event

    //BeginRequest implementation
    public void OnBeginRequest(Object sender, EventArgs e)
    {
    HttpApplication app = (HttpApplication)sender;
    string HttpUrl = app.Request.Url.ToString();

    if (HttpUrl.StartsWith("http:"))                                       //Redirection done only if URL starts with http:
    {
       HttpUrl = HttpUrl.Replace("http:", "https:");
       app.Response.Redirect(HttpUrl.ToString(), true);      //Redirecting (http 302) to the same URL but with https
       app.Response.End();                                                   //We don't want to any further so end
    }
    }

  5. Make sure you have the following in your web.config inside configuration tag
    <system.webServer>
    <modules>
    <add name="redir" type="http2https.redir" />
    </modules>
    </system.webServer>

Your http to https redirection sample is ready!!!


How to deploy the HttpModule
There are multiple ways you can deploy this component (I'm assuming that it's being deployed for "default website")

Method 1
Create a folder called "App_Code" inside "%systemdrive%\inetpub\wwwroot"
Copy "redir.cs" file into "App_Code" folder
Copy "web.config" file inside "%systemdrive%\inetpub\wwwroot"

Method 2
Create a folder called "bin" inside "%systemdrive%\inetpub\wwwroot"
Compile "redir.cs" into "redir.dll" and copy it into "bin" folder (to compile -> csc.exe /out:redir.dll /target:library redir.cs)
Copy "web.config" file inside "%systemdrive%\inetpub\wwwroot"

If you open IIS7 UI and go to Modules you can see your HttpModule listed there.


 

Redirection options for prior versions of IIS below (you need to search for these)

- ASP.NET URL mapping capability (HttpContext.RewritePath)
- IIS v6.0 Resource Kit includes the UrlRemap tool
- IISRewrite
- ISAPI_Rewrite - includes a "lite" version available for free.
- Mod_Rewrite 2
- Ionic's ISAPI Rewrite Filter

Comments (3)

  1. I had written a sample to redirect all http traffic to https (secure) in September 2006 http://blogs.msdn.com/sukeshak/archive/2006/09/03/http2https.aspx

  2. Amit says:

    First of all thanks for taking the time to write this. I'm using IIS 7 and here's my scenario, I have the following folders that need to use SSL and the rest of the website does not,

    1. Login

    2. Secure

    3. Admin

    Anonymous user needs to be redirected from http to   folder LOGIN(https) then once the user is authenticated heshe should be able to bounce back and forth between http and https pages. How can I accomplish this or is this the wrong approach? Any help with this will be greatly appreciated, thank you!

  3. Sukeshak says:

    hi Amit,

    You can do this either using a module like I have above or through normal code…

    Please send me an email on sukesh at awesomeideas dot net, if you need anything else…

Skip to main content