Http to https redirect htaccess code

If the SSL certificate is added to your domain it is important to use https protocol for the security of your website.
HTTP - Hypertext Transfer Protocol
SSL - Secure Socket Layer

Before editing the .htaccess file one should take a backup to avoid unexpected break on the site.

For example if your website should be loaded as the following rule should be added to .htaccess

Redirect using default rule

Once this rule is added to .htaccess all http request will be forced redirect to https.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

Redirect the site using the port number

 RewriteCond %{SERVER_PORT} ^80$
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Redirect adding your site url

Here you can add the static domain url instead of SERVER_NAME.

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

After adding the https redirect rule the .htaccess file should look like the below example.

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Where can I get the htaccess file in cpanel?

  1. Login to cPanel with your username and password
  2. Go To File Manager > Document Root (your website root folder)
  3. If the file is not visible check "Show Hidden Files" option.