Welcome to MSDN Blogs Sign in | Join | Help

Redirecting from http to https in IIS7 (http2https Updated)

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

In one of our internal discussion alias the question came up that this method does not work when SSL is forced on the website. Step 5 below handles that scenario by checking the "403.4 SSL required" response and handling it during OnEndRequest event.

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); 
    application.EndRequest += new EventHandler(OnEndRequest);
     
  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. Add the following method to implement "OnEndRequest" event

    //This is for scenario where SSL is forced on the site
    public void OnEndRequest(Object sender, EventArgs e)
    {
      HttpApplication app = (HttpApplication)sender;
      if (app.Response.StatusCode == 403 && app.Response.SubStatusCode == 4)
      { 
        string HttpUrl = app.Request.Url.ToString();
    
        if (HttpUrl.StartsWith("http:"))
        {
            HttpUrl = HttpUrl.Replace("http:", "https:");
            app.Response.Redirect(HttpUrl.ToString(), true);
            app.Response.End();
        }
    }
    
    

  6. 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 and also works if you force SSL!!!


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.

Source code attached with this post

Published Wednesday, September 05, 2007 3:07 AM by sukeshak
Filed under:

Attachment(s): http2https.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

# MSDN Blog Postings &raquo; Redirecting from http to https in IIS7 (http2https Updated)

Friday, September 07, 2007 2:02 AM by Steve Schofield's Blog

# IIS7 post #50 - http to https redirect module

Another popular question in the newsgroups. This example is targeted for IIS7. http://blogs.msdn.com/sukeshak/archive/2007/09/05/redirecting-from-http-to-https-in-iis7-http2https-updated.asp

Wednesday, September 19, 2007 9:52 AM by Chris Love's Official Blog - Professional ASP.NET

# Links of the Week September 19 2007

Everyone seems to be coming off vacation and my book is done so now to list the resources to make us

Thursday, November 01, 2007 9:51 AM by John Rummell

# re: Redirecting from http to https in IIS7 (http2https Updated)

Does this require IIS7?  Or could it work on IIS6 as well?

Wednesday, November 07, 2007 9:24 PM by sukeshak

# re: Redirecting from http to https in IIS7 (http2https Updated)

This sample is illustrated for IIS7 which make use of new integrated pipeline.

Tuesday, December 11, 2007 12:41 AM by Ram

# re: Redirecting from http to https in IIS7 (http2https Updated)

Hi mate,

I tried this code..geting few errors.can you please check it.

Cheers,

Ram

Tuesday, December 11, 2007 12:49 AM by sukeshak

# re: Redirecting from http to https in IIS7 (http2https Updated)

Please add more details about the error and i'll see how I can help.

Thursday, November 20, 2008 4:34 PM by Mark

# re: Redirecting from http to https in IIS7 (http2https Updated)

great help, works very well. THANKS

Thursday, January 22, 2009 8:47 PM by Raj

# re: Redirecting from http to https in IIS7 (http2https Updated)

Can this work for OWA 2007 also?

Wednesday, April 01, 2009 2:52 AM by sukeshak

# re: Redirecting from http to https in IIS7 (http2https Updated)

The sample provided here working in Integrated Pipeline mode. AFAIK, OWA 2007 still uses Classic Pipeline so you would need a native component in that case.

You can try using IIS7 URL Rewrite module if you are asking for IIS7.

http://www.iis.net

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker