Where are the secure server files located? Okay, mine are co-located which makes it far more complex as you’ll have to be very careful with your redirections. If your secure server certificate is for the www’d domain name, you MUST make the redirection but, if for the non-www’d domain name, you must NOT make the redirection.
I’ve found (years ago) that using the {SERVER_PORT} variable is the safest way to differentiate a secure request from a non-secure request ({HTTPS} does not exist for non-secure):
# .htaccess in DocumentRoot where http and https code is co-mingled
RewriteEngine on
# Port 80 is http
RewriteCond %{SERVER_PORT} ^80$
# fetch properly escaped host
RewriteCond %{HTTP_HOST} ^domain\\.com [NC]
# use existing {REQUEST_URI} for redirect
RewriteRule .? http://www.domain.com%{REQUEST_URI} [R=301,L]
# Port 443 is https
RewriteCond %{SERVER_PORT} ^443$
# fetch properly escaped host
RewriteCond %{HTTP_HOST} ^domain\\.com [NC]
# use existing {REQUEST_URI} for redirect
RewriteRule .? https://www.domain.com%{REQUEST_URI} [R=301,L]
Alternatively, some “cutsie” code:
RewriteEngine on
RewriteCond %{HTTP_HOST}/s%{HTTPS} ^(www\\.)([^/]+)/((s)on|s.*)$ [NC]
RewriteRule . http%4://%2%{REQUEST_URI} [R=301,L]
Explanation: I needed an explanation so here it is:
/s%{HTTPS} will create the string /son or /s , depending on the value of HTTPS.
/((s)on|s.) In this REGEX, %3 will always be matched and set, because son matches (s)on, and other cases match s..
When (s)on is matched, %4 will be set to ‘s’.
In short, this is a trick to replace {HTTPS}'s "on" with "s". If you followed this, GREAT! If not, wait a week and look at it again without the explanation. If you still understand it, use it, otherwise the first set of code is easier to understand and, therefore, easier to maintain.