Thursday, August 16, 2012

How to redirect from HTTP to HTTPS in ASP.NET

If you are a web developer and want to make an online shopping site, you will definitely asked to use SSL and use HTTPS request. Now the linked question in your mind is How do i redirect request from HTTP to HTTPS?

Here is the simple solution.

Go to Web.Config file and add below code stuff.

<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}" />

    </rule>

  </rules>

</rewrite>

That's great your all request are now redirect to HTTPS. Wait this may not be happened with all the guys. Please make sure you have installed the URL Rewrite Module 2.0 available here.  If it is not installed and you try above code it will make your web.config file invalid and raise error.

After installing Rewrite Module 2.0 restart the IIS and your url redirect will start working.