Force http to https with cPanel

danielpmc

New member
You need to have a SSL certificate in place before using the shown codes. Place one or the other in your cPanel public_html/.htacces file. Important: any .htaccess file must start with a dot and the chmod (pernmissions) are ALWAYS set at 644

If the .htaccess file does not have the "RewriteEngine on" in the file, uncomment by removing the hashtag. This only needs to appear once in the .htaccess file as it will cover all other Rewrites present and future.
Replace example dot com with your domain name

This forces all http pages to No WWW https
Code:
#RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

This forces all http pages to WWW https
Code:
#RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
 
hello

about your post you can use :

Code:
  RewriteCond %{SERVER_PORT} 80
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Regards
 
If the server doesn't have "Rewrites" enabled by default, then your code will no work. You have commented out the first line (using the #).

It's always best to enable that, forcing the rewrites ON and then continuing with your rewrite statements.

You could also wrap the whole things in an IF MODULE statement for even better verification that the module is enabled.
 
If the .htaccess file does not have the "RewriteEngine on" in the file, uncomment by removing the hashtag. This only needs to appear once in the .htaccess file as it will cover all other Rewrites present and future. Replace example dot com with your domain name

This is in my OP.
 
You could also wrap the whole things in an IF MODULE statement for even better verification that the module is enabled.

Is this what you mean?

Code:
<IfModule mod_version.c>
<IfVersion >= 2.4>
SSL Code here
</IfVersion>
</IfModule>

<IfModule !mod_version.c>
<IfModule !mod_authz_core.c>
SSL Code here
</IfModule>

<IfModule mod_authz_core.c>
SSL Code here
</IfModule>
 
Last edited:
All examples shown above do not require any modules to be enabled beyond RewriteEngine on. They are forcing all traffic regardless if it is http/https coming through port 80 to force a redirection to its counterpart of https. These are not contingent on any SSL module to be enabled, as they are redirection codes to be used in a .htaccess file.
 
There is a potential, yet very slight, SEO redirect issue which is reported in Google Webmaster accounts for using this closing tag: [L,R=301]

Why: Because Google generally does not care for hard coded internal page redirects. Yes, some it accepts others it doesnt.

However, Google accepts this tag [R,L] as a soft coded redirect. I have not received a warning for using this tag.
 
Back
Top