Redirecting HTTP to HTTPS in .htaccess: Migration Guide

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading...

If you are reading this article, we can safely assume that you have already purchased an SSL certificate and looking forward to installing it. If you are still struggling in choosing the right SSL for your website, check out this article to understand the different SSL types and select the best-suited SSL with cost-efficient options.

Secure a WordPress Website with An SSL Certificate – Save 87%

Save 79% on a highly trusted Positive SSL, an ideal solution for WordPress CMS websites. It includes unlimited server licenses, reissuances, 256-bit encryption, and more.

Shop for Positive SSL and Save 87%

If you are looking for help redirecting HTTP to HTTPS on a WordPress site, consider our expert guide on How to Install An SSL Certificate on WordPress.

HTTP to HTTPS Redirection Guide with .htaccess

Locate .htaccess in cPanel File Manager

  • Go to cPanel
  • Click on File Manager under ‘Files.’
Files Option in cPanel
  • Select the option for Document Root for. Select the domain name from the drop-down menu. OR click on ‘Settings’ on the upper right side of the page if you have only one website.
  • Locate the .htaccess file
  • The .htaccess file is always hidden. So, check “Show Hidden Files (dotfiles)”
Preference Option in cPanel
  • Right-click on the .htaccess file and click on Edit
  • A dialogue box will pop up. Click on Edit again.
Edit Option in cPanel
Edit Option in cPanel for htaccess file

Before editing the code, there are a few things you should take into consideration:

  • Your .htaccess file must be stored in the website’s root folder or in the same folder where your site is located.
  • Search for the word RewriteEngine On in .htaccess file to avoid repetition. If RewriteEngine On is already written in the .htaccess file, add the below code immediately after it (don’t duplicate the RewriteEngine On line)
  • Type your own domain name in place of yoursite.com

.htaccess Code for HTTP to HTTPS Migration Without WWW

If you want your website’s non-www version to run on HTTPS (i.e., HTTPS://yoursite.com), add these lies in the .htaccess file:

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

.htaccess Code for HTTP to HTTPS Migration for WWW Version

If you are redirecting the WWW version of your domain to HTTPS (i.e., HTTPS://www.yoursite.com), add the below code to the .htaccess file

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.yoursite.com [NC]
RewriteRule ^ HTTPS://www.yoursite.com %{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP:X-Forwarded-Proto} !HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ HTTPS://yoursite.com %{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect All Web Traffic from HTTP to HTTPS

If you want to redirect all web traffic to HTTPS (instead of redirecting to a specific site/webpage/folder), insert these codes.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ HTTPS://www.yoursite.com/$1 [R,L]

Redirect a Particular Domain from HTTP to HTTPS

If you have multiple domains and want to redirect only a specific domain from HTTP to HTTPS, insert following codes.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yoursite\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ HTTPS://www.yoursite.com/$1 [R,L]

Redirect Only a Specific Folder (webpage) to HTTPS

For some reasons, people choose to keep only some webpages on HTTPS, while keeping other pages on HTTP. To Redirect a specific folder/webpage to HTTPS, write following code:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ HTTPS://www.yoursite.com/folder/$1 [R,L]