Author : Guest
Total Views : 419
Posted On : Thu, Apr 5, 2018
Updated On : Thu, Apr 5, 2018

HTTP to HTTPS redirection using Rewrite module in web.config

Most of the websites often run on the HTTP but when we make them more secure and apply https, we have required a way to redirect the user to https if he accidentally visits the http version of your website.

In this article, you will see the different ways to redirect the user to https over HTTP.

Using Rewrite module

By using the rewrite module we can redirect the user to https just by creating a rule in web.config based on a particular URL pattern.

Following is the rule which redirects a use if he has visited an HTTP link.

<?xml version="1.0" encoding="UTF-8"?>
                <clear />
                <rule name="Redirect to https" stopProcessing="true">
                    <match url=".*" />
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />

Note: Before using the above code you must install the rewrite module on the server. The above example shows that how you can use rewrite module to create the redirection. It has nothing to do with the server type, the server type can be an IIS/Apache etc. Rewrite module process the request at lower levels before request moves further and hits the point where your code gets executed.

Using JavaScript

We can also redirect the user to https from HTTP using javascript. But for this, one will have to put the javascript on every page. Following is javascript snippet for redirection

<script language="JavaScript">
    function redirectToHttpsfromHttp()
        var location = window.location;
        var httpURL= location.hostname + location.pathname +;
        var httpsURL= "https://" + httpURL;
        window.location = httpsURL;