Redirecting HTTP Request To HTTPS Using Web.config File

In previous article, we saw how to add an SSL certficate to Azure Web App. Even if you have an SSL certificate, it doesn’t mean that all your users are going to use the same one.  For that, you have to redirect users to the HTTPS while they try to access HTTP.

When someone checks out websites with http and https, they get the Web app. Now, what we want is to redirect the users who use http to the https link.


Let us see how to make it possible. Here, I am going to open the project I made in a previous article where I deployed a Web app using Visual Studio.

 

On Solution Explorer, you can see the Web.config file.

 

Add the following code snippet to the Web.config file. Open the file to get it done. You just need to add a piece of code to your Web.config file.
  1. <system.webServer>    
  2.    <rewrite>    
  3.       <rules>    
  4.       <rule name="Force HTTPS" enabled="true">    
  5.    <match url="(.*)" ignoreCase="false"/>    
  6.    <conditions>    
  7.       <add input="{HTTPS}" pattern="off"/>    
  8.    </conditions>    
  9.    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent"/>    
  10.       </rule>    
  11.       </rules>    
  12.    </rewrite>    
  13. </system.webServer>    

What we are doing is just rewriting - when somebody comes to http, redirect them to https.

 

Publish your profile and try to access the Web app using HTTP. Here, you see that it will automatically redirect to HTTPS.

 
 


Similar Articles