I have this code for forcing SSL on a website generally:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
To avoid using the domain or subdomain names I used to use the following code:
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I prefer without WWW.
%{HTTPS} Set to on if the request is served over SSL.
%{HTTP_HOST} Everything between the protocol (http:// or https://) and the request (/example-file.txt).
%{REQUEST_URI} Everything after the server address — for example, a request to http://example.com/my/example-file.txt sets this value as my/example-file.txt.
%{HTTP:X-Forwarded-Proto} The protocol used when the URL was requested — set to http or https.
I hope it can be useful for anybody.
For debugging I use a website I found http://martinmelin.se/rewrite-rule-tester/
Using the rules you provided
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I got an error at
RewriteCond %{HTTPS} !=on
This might work for you
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]
Found the above example at https://www.sslshopper.com/apache-redirect-http-to-https.html