How to force redirect http to https using htaccess

SSL or HTTPS, it has become a major requirement for websites.Almost all web panels such as cPanel, CentOS-webpanel and vestacp facilities free Letsencrypt SSL. In addition, SSL is also an important consideration by many search engines like google in determining the ranking.

But the question arises “how to redirect http to https so that Google can index web pages we are already equipped ssl?”

First Choice
By checking whether https on or off as follows:

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

Second Chioce
By checking whether the port is used. in this case we consider http using port 80.

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$$1 [R,L]

If you use http using port other than port 80 (for example, port 8080), you need to adjust the script above by replacing the port number 80 to 8080

Leave a Reply

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