HTTP to HTTPS Redirect using .htaccess or web.config

To redirect your website from HTTP to HTTPS automatically, you need to place the following code using an  .htaccess or web.config file. 

Linux  – Force HTTPS using .htaccess

You should use the following code within the  .htaccess file for your website.  This file should be placed within the root of your website.  This is normally the same location as your websites index page.  This code forces users to redirect to an HTTPS connection.

RewriteEngine on
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.domainname.com%{REQUEST_URI} [L,R=301]


Windows  – Force HTTPS using web.config

You should use the following code within the web.config  file.  This file should be placed within the root of your website files.  This is normally the same location as your websites index page.   This code forces users to redirect to an HTTPS connection.

<configuration>
<system.webServer>
<rewrite>
    <rules>
       <rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
         <match url="(.*)" /> 
         <conditions> 
           <add input="{HTTPS}" pattern="off" ignoreCase="true" />
         </conditions> 
         <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
       </rule> 
    </rules>
</rewrite>
</system.webServer>
</configuration>

If you have any questions please contact us

Was this answer helpful? 0 Users Found This Useful (256 Votes)