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. 

Related Articles

How to install Codeignetor centos 7?

Codeignetor is a PHP framework used to build a web application that is used widely by the...

How to set up Apache password Authentication on Ubuntu 18.08?

On the internet, almost all the applications have some parts which should be protected with a...

How to install Nagios 4.4.5 on Centos7?

Nagios is an Open-source monitoring tool. With Nagios, you can monitor your servers and networks...

How to Move Accounts From One cPanel Server To Another cPanel server?

cPanel facilitates you with the built-in Transfer Tools which allow you to migrate your existing...

How to determine what is using the most disk space on a Linux server?

There are times when you receive following error while accessing the cPanel WebMail or WHM. You...

  • 0 Users Found This Useful

Was this answer helpful?