Redirecting from httpd to https

Enabling redirect for all sites in a cPanel account

To redirect all sites within a cPanel account, one of the following blocks should be added to .htaccess:

A
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

This block enables rewriting capabilities, verifies that the initial request does not already have https://, and rewrites the entire requested URL, replacing http:// with https:// (e.g., http://domain.com/subfolder/index.php will be replaced with https://domain.com/subfolder/index.php).

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

This block works the same as the previous one, just with the help of a different syntax. It is possible to use either of the above mentioned rewrite rules in order to redirect all sites within a cPanel account.

Forcing HTTPS for a specific site

To enable a redirect for a single site, one can specify any of the blocks listed below in .htaccess:

A
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^ncssltest\.info$ [OR]
RewriteCond %{HTTP_HOST} ^www\.ncssltest\.info$
RewriteRule ^(.*)$ https://www.ncssltest.info/$1 [R,L]

B
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^ncssltest\.info$|^www\.ncssltest\.info$
RewriteRule ^(.*)$ https://www.ncssltest.info/$1 [R,L]

C
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?ncssltest\.info
RewriteRule ^(.*)$ https://www.ncssltest.info/$1 [R,L]

Generally, all of the rule sets above do the same job: checking the port (80 for http, 443 for https), verifying whether a domain name in the initial request is with or without “www.” alias, and rewriting the URL with https://. You may choose which one to implement based on your individual preference.