-1

I am trying to configure .htaccess to redirect all "http" to "https" - except subdomains (but still include 'www' and 'static').

Ex: http://abc.com -> https://abc.com

http://www.abc.com -> https://www.abc.com

http://static.abc.com -> https://static.abc.com

http://hello.abc.com -> http://hello.abc.com

http://hi.abc.com -> http://hi.abc.com

http://bye.abc.com -> http://bye.abc.com (please note that I am creating random subdomains... thus I can't exclude a given number of subdomains)

I am literally pulling my hair out for hours on this issue.

1,000 thanks for your help!! Damien

  • It might help to include what you've tried and what's going wrong. – showdev Mar 24 '20 at 19:01
  • Would probably make more sense to set up a wildcard certificate, so that you _can_ serve these subdomains via HTTPS as well. What’s the point in making anything only “half secure” these days. – CBroe Mar 25 '20 at 07:39

1 Answers1

0

This .htaccess should do the job

RewriteEngine On
RewriteCond %{SERVER_PROTOCOL} ^http$
RewriteCond %{HTTP_HOST} ^(www|static)\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

Explanation

RewriteCond %{SERVER_PROTOCOL} ^http$ Condition is met if protocol is http (not https)

RewriteCond %{HTTP_HOST} ^(www|static)\. Condition is met if protocol if subdomain is either www or static

This will redirect

http://www.example.com -> https://www.example.com
http://www.example.com?param=value -> https://www.example.com?param=value
http://www.example.com/something -> https://www.example.com/something
http://static.example.com -> https://static.example.com
http://static.example.com?param=value -> https://static.example.com?param=value
http://static.example.com/something -> https://static.example.com/something

But won't redirect

http://hello.abc.com
http://hi.abc.com
http://foo.abc.com

Demo

Stephane Janicaud
  • 3,531
  • 1
  • 12
  • 18