I'm trying to redirect all HTTP traffic to HTTPS including subdomains. I have the domain setup with FastComet on their Shared Hosting using cPanel. Some examples of how I like to redirect.

http://www.example.com      ->     https://www.example.com
http://example.com          ->     https://example.com
http://www.example.com/mail ->     https://www.example.com/mail
http://example.com/mail     ->     https://example.com/mail
http://mail.example.com     ->     https://mail.example.com

I've tried adding below to my .htaccess file

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(.+\.)?mydomain\.com$
RewriteRule ^(.*)$ https://%1mydomain.com/$1 [R=301,L]

And the more general

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

But none of them redirects http://mail.example.com to https://mail.example.com while redirecting http://example.com/mail to https://example.com/mail works. What is it I'm missing my setup?

  • 1
    Where did you place that htaccess file? if it's located in the root of example.com it will only redirect for that domain. You'd have to place a separate file on mail.example.com. If you want such a general redirect it's usually the best to include it in your server configuration rather than htaccess file. – Seth Jul 6 '17 at 6:31
  • Currently I have it in the public_html folder. The public_html folder is in the root. Where do I find the server configuration? – g3blv Jul 6 '17 at 7:14
  • 1
    If you're on shared hosting, it's unlikely that you will be able to access it directly. It's usually somewhere in /etc. You'd need to change the VirtualHost (likely) see also: http to https apache redirection. If you're not able to do that, you will have to use a htaccess file for every domain. – Seth Jul 6 '17 at 7:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.