How to redirect your website from HTTP to HTTPS using .htacess?

SSL certificate plays a significant role on your website. It helps to build the trust of your visitors as well as encrypts the data that is shared by your customers on your website. The data such as a customer’s personal details, credit/debit card information, bank details, etc. are secured with SSL. Additionally, having the SSL certificate installed on your website also helps in boosting your website’s ranking in the search engines.

SSl-TSL-Status
HostingSewa offers SSL certificate with its entire web hosting plans. You can activate this SSL in your Control Panel under Security > SSL/TSL or SSL/TSL Status.

So, the visitors on your website should be accessing it using an SSL-encrypted connection for additional security, accessibility or PCI compliance.

HostingSewa offers SSL certificate with its entire web hosting plans. You can activate this SSL in your Control Panel under Security > SSL/TSL or SSL/TSL Status.

You can force visitors to use SSL through your .htaccess file using mod_rewrite.

Changing site URL to HTTPS with .htaccess

.htacess HTTP to HTTPS
How to redirect your website from HTTP to HTTPS using .htacess?

1. Redirect all pages to HTTPS

If you want to force all web traffic to use an SSL certificate, run the following code in the .htaccess file in your website’s root folder.

NOTE: If there is an existing code in your .htaccess, add this code above where you will find rules with a similar starting prefix. If there are any issues afterward, move the new code below some elements – e.g. Options +FollowSymLinks.

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

2. Force Specified Domain Redirection

In order to force a specific domain for using HTTPS, you can use the below code in the .htaccess file in your website’s root folder:

NOTE: If there is an existing code in your .htaccess, add this code above where you will find rules with a similar starting prefix. If there are any issues afterward, move the new code below some elements – e.g. Options +FollowSymLinks.

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

If the above code isn’t working, remove the first two lines and run the code as below:

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

Ensure that you replace example.com with the domain name for which you are forcing https. Also, you need to replace www.example.com with your actual domain name.

3. HTTPS Redirection of Specified Folder

In case you want to force SSL on a specific folder, input the below code into a .htaccess file that is placed in that particular folder:

NOTE: If there is an existing code in your .htaccess, add this code above where you will find rules with a similar starting prefix. If there are any issues afterward, move the new code below some elements – e.g. Options +FollowSymLinks.

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.example.com/folder/$1 [R=301,L]

Ensure that you change the folder reference to the original folder name. Then make sure to replace www.example.com/folderwith your original domain name and folder, you want to force the SSL on.

In this way, you can easily change your website from HTTP to HTTPS using the .htaccess.

Leave a Reply

Your email address will not be published. Required fields are marked *