Redirect from HTTP to HTTPS in ASP.NET


You can redirect from HTTP to HTTP request in ASP.NET website using the web.config in ASP.NET web sites.

Open web.config file in the root directory of your website and add the following section in the section.

Example: Redirect from HTTP to HTTPS in web.config
<rewrite>
    <rules>
        <rule name="HttpToHttps" stopProcessing="true">
            <match url=".*" ignoreCase="true" />
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
            <action type="Redirect" redirectType="Permanent" url="https://www.mywebsite.com/{R:0}" />
        </rule>
    </rules>
<rewrite>

You can remove redirection if you are running it in your development machine localhost by adding one more condition, as shown below.

Example: Ignore localhost
<rewrite>
    <rules>
        <rule name="HttpToHttps" stopProcessing="true">
            <match url=".*" ignoreCase="true" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="localhost" negate="true"/>
                <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
            <action type="Redirect" redirectType="Permanent" url="https://www.mywebsite.com/{R:0}" />
        </rule>
    </rules>
<rewrite>

Redirect to HTTPS in ClouldFlare

You can redirect from HTTP to HTTP from Cloudflare. Go to SSL/TLS -> Edge Certificates and turn on the Always Use HTTPS.

Redirect to HTTPS in CloudFlare

Redirect to HTTPS in Azure

If your website is hosted on azure then you can redirect from HTTP to HTTP requests by configuring your app service.

Select your app service and go to configuration -> General settings tab and select HTTPS Only flag to On.

Redirect to HTTPS in Azure