Attention: We have retired the IIS.NET Community Blogs. Learn more >

Contents tagged with HTTP amp; IIS

  • 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://www.awesomeideas.net/post/2006/09/03/Redirecting-from-http-to-https-in-IIS7.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

  • 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 .