How to redirect http to https using .htaccess?

If you have installed SSL Certificate on your website and want to redirect your website traffic from http to https using .htaccess then you need to add the below code to your .htaccess file for the redirection. 

Redirecting HTTP to HTTPS

Add the following line to your .htaccess, if you are looking to redirect a specific domain from HTTP to HTTPS.

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

Add the following line to your .htaccess, if you are looking to redirect a specific folder from HTTP to HTTPS.

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

Note: You will need to replace example.com with your domain where required in your code and set the folder name where it is required. 

  • 0 Users Found This Useful

Was this answer helpful?

Related Articles

How to Kill a Process Running on Specific Port in Linux?

First of all, we will need to identify the Process ID which runs on any port. Once we find the...

How to Disable SELinux on CentOS?

SELinux called Security-Enhanced Linux is a security module of Linux kernel which provides...

Is it possible to take complete VPS backup in VPS itself?

Please note that, there is no provision to take backup of full VPS image in VPS itself. As an...

Is HTML5 Supported in Your Servers?

Since HTML content has nothing to do with the web server, there are no special requirements...

How to install ISPConfig on CentOS 7.x?

This article will help you on how to install ISPConfig on CentOS 7.xISP config is an open-source...