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?

  • 3
    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
  • 2
    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

If this is a reasonably recent Apache. This ability can be achieved in extra/http-vhosts.conf. But your ability to accomplish the following in .htaccess will be completely dependent on your hosting providers willingness to let you do so.

If you place the following in the .htaccess file at the root of your server(s). It should accomplish your goal:

    ServerName MyServer.COM
    Redirect permanent / https://myserver.com/

All this does is send a 301 permanent. So you won't get penalized by google (if you care), and browser bookmarks, and other clients will become cluefull of the change.

It's pretty simple to understand. So I won't elaborate further.

Good luck!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.