HTTP to HTTPS Redirection Automatically Using .htaccess and web.config

htaccess-http-https

Hello Friends, Today we will learn about “How we can Redirect visitors to our HTTP website to HTTPS“.

If you have SSL Secure Certificate installed on your website, you can easily redirect visitors to the Secured version of your website(HTTPS) automatically. First, we have to know that where your website is hosted.

Linux & cPanel

If you don’t have a .htaccess file in the root directory of your website then you have to create a new file. If you don’t know how to create this, watch the video:

Once you create the .htaccess file in the file manager, just copy & paste this code in the .htaccess file.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

NOTE: If you already have an .htaccess file, then check these points

  • Don’t duplicate RewriteEngine On
  • Make sure the lines starting from RewriteCond and RewriteRule immediately follow the already-existing RewriteEngine On.

If you have confusion about how to add this code in existing .htaccess file then see the screenshot of a WordPress site below.
htaccess HTTPS RewriteCond RewriteRule

Windows & Plesk

Windows hosting accounts use web.config file instead of .htaccess to handle redirections.

NOTE: If you don’t have web.config file, then the process will be same like .htaccess, just type web.config while creating a new file in file manager

Using below code in your web.config file, you can automatically redirect visitors to your HTTPS version website.

<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>

NOTE: If you already have a web.config file, then check these points

  • Ensure you have sections (i.e. opening and closing tags) for:
    ~ system.webServer (which contains rewrite)
    ~ rewrite (which contains rules)
    ~ rules (which contains one or more rule sections)
    Insert any of those sections that do not exist.
  • Insert the entire rule section, including match, conditions, and action, inside the rules section.

Share:

Facebook
Twitter
Pinterest
LinkedIn

2 thoughts on “HTTP to HTTPS Redirection Automatically Using .htaccess and web.config”

    1. Hello Robert,

      First of all, thank you so much for giving an interest in my blog post.
      Secondly, you asked a good question, Let me explain.

      This blog post is related to the redirection of HTTP website to HTTPS one. Either your website URL is with WWW or without WWW, your website URL always have HTTP. Browers Address bar never saw you http in front of your domain(www.domain.com or domain.com) name until you have https.

      You have to update your DNS with both http://www.domain.com and domain.com

      Please follow us and let me know your feedback.
      Thanks

Please share your thought