Site Loader
Get a Quote

Force HTTP to https

Force HTTP to https in asp.net c#

Force HTTP to https: Hi Readers, If you have purchased an SSL certificate from a web hosting company to make your website secure then your website always will be open with HTTPS. But when you will open your website after purchasing SSL, it will open default with HTTP to make default to HTTPS .you have to write a rewrite rule in the web.config file in the root folder to force website open with HTTPS.

 

Rewrite Rule

Here is an example – it will force HTTPS to all pages of the website .Which was opening with HTTP

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

how to append above code in web.config file

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
 <appSettings>
 <add key="CONSTR" value=""/>
 <add key="CONSTR" value=""/>
 </appSettings>
 <connectionStrings>
 <remove name="LocalSqlServer"/>
 <add name="CONSTR" connectionString="Data Source=""/>
 </connectionStrings>
 <system.web>
 <securityPolicy>
 </securityPolicy>
 <httpRuntime />
 <compilation defaultLanguage="c#" debug="true">
 </compilation>
 <pages validateRequest="false" />
 <customErrors mode="Off" />


 </system.web>
 <system.webServer>
 
 <rewrite>
 <rules>
 <clear />
 <rule name="Redirect to https" stopProcessing="true">
 <match url=".*" />
 <conditions>
 <add input="{HTTPS}" pattern="off" ignoreCase="true" />
 </conditions>
 <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
 </rule>
 </rules>
 </rewrite>

<security>
 <requestFiltering>
 <requestLimits maxAllowedContentLength="1073741824" />
 </requestFiltering>
 </security>
 <httpErrors errorMode="Detailed" />
 </system.webServer>


</configuration>



Post Author: TechieFlair

Leave a Reply

Your email address will not be published.