Click here to Skip to main content
15,881,709 members
Articles / Web Development / IIS

How to Redirect HTTP to HTTPS with the IIS Rewrite Module

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Feb 2020CPOL2 min read 8.3K   4   3
The IIS rewrite module is a really powerful feature in IIS. It gives you power to set up rules to handle how requests for specific URLs are handled.
This article looks at the IIS rewrite module to and shows how set up rules to handle how requests for specific URLs are handled. It looks at a Powershell script that calls the Web Platform Installer to install the URL Rewrite 2.0 feature, and how to load up your web.config.

The IIS rewrite module is a really powerful feature in IIS. It gives you power to set up rules to handle how requests for specific URLs are handled.

You can:

  • Perform redirects
  • Send custom responses
  • Stop HTTP requests based on the rules in the rewrite module.

Redirecting to HTTPS

There are multiple ways in IIS to redirect a URL to HTTPS. The HTTP Redirect feature is useful, but it only redirects to a specific URL. If you want to redirect to HTTPS and retain the full URL requested with page and querystring I prefer to use the URL Rewrite module.

Installing IIS URL Rewrite Feature

The URL Rewrite module works with IIS 7 and above, it’s currently on versioon 2.0.

You can install the URL Rewrite 2.0 module using:

Web Platform Installer

Web Platform Installer. Or directly from it’s page at http://www.iis.net/downloads/microsoft/url-rewrite

Chocolatey

https://chocolatey.org/packages/UrlRewrite This does require IIS (obviously I hope?)

choco install urlrewrite

Powershell

The following Powershell script does the following: .Creates an msi directory on the c: .Downloads the Web Platform Installer .Installs the Web Platform Installer .Calls the Web Platform Installer to install the URL Rewrite 2.0 feature

reate-Item c:/msi -Type Directory
Invoke-WebRequest 'http://download.microsoft.com/download/C/F/F/CFF3A0B8-99D4-41A2-AE1A-496C08BEB904/WebPlatformInstaller_amd64_en-US.msi' -OutFile c:/msi/WebPlatformInstaller_amd64_en-US.msi
Start-Process 'c:/msi/WebPlatformInstaller_amd64_en-US.msi' '/qn' -PassThru | Wait-Process
cd 'C:/Program Files/Microsoft/Web Platform Installer'; .\WebpiCmd.exe /Install /Products:'UrlRewrite2' /AcceptEULA /Log:c:/msi/WebpiCmd.log

Setting up the rule

If you load up IIS you will now see the URL Rewrite Module.

–Screen shot

Click into it and you are greeted with the following screen.

– screen shot

You can set up the rules in here, but I actually prefer directly in the web.config. I think it allows you to understand the rules better than the GUI shows.

So load up your web.config and add the following new section:

XML
<rewrite>
    <rules>
        <rule name="Redirect to HTTPS" stopProcessing="true">
            <match url="(.*)"/>
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$"/>
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
        </rule>
    </rules>
</rewrite>

Now if you go back to the URL Rewrite module in IIS you will see how it’s set up the rules.

Summary

The URL Rewrite module is a powerful feature than gives you full control of what is going on.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
I have been working in software development for over 16 years, during that time I have worn many hats.

I have worked as a Software Engineer, Architect, Agile Coach and Trainer. I’ve created teams, I’ve lead teams, but my main goal is to help teams build great software and enjoy the process.

I help a whole range of businesses – from startups with just an idea who want to build a team to take that idea into reality and FTSE 100 businesses who need to optimise existing teams – I train, mentor and coach them to success.

If you happen to know of anybody who could benefit from results like this, then please go to my contact page and get in touch.

Owen Davies

Comments and Discussions

 
QuestionJust a note ... Pin
Richard MacCutchan17-Feb-20 0:34
mveRichard MacCutchan17-Feb-20 0:34 
QuestionLocation Header? Pin
Randor 11-Feb-20 13:17
professional Randor 11-Feb-20 13:17 
AnswerRe: Location Header? Pin
MadMyche11-Feb-20 15:14
professionalMadMyche11-Feb-20 15:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.