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)
Download and Install IIS7 Managed Module Starter Kit
(Not really a requirement but it would make developing IIS7 modules easier)Rename the default class name created to "redir.cs" and rename project/solution/namespace to "http2https"
Add the following code in "Init" method
// register for the BeginRequest event
application.BeginRequest += new EventHandler(OnBeginRequest);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
}
}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
Anonymous
September 04, 2007
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.aspxAnonymous
January 28, 2010
The comment has been removedAnonymous
July 06, 2010
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...