I am trying to redirect all www and non-https requests to the HTTPS only versions with Apache server. I don't need or want www or non SSL URLs at all. The issue is that with the 2 different snippets below I only get correct http to https redirect but I can still get www to be accessible on sub-domains. The primary domain may only work as it is a Wordpress site and it does it's own redirects. What am I doing wrong here? Should I have ServerAlias with www version of the domain and sub-domains or just non-www ServerName?
# RewriteCond %{SERVER_NAME} =example.com [OR]
# RewriteCond %{SERVER_NAME} =*.example.com [OR]
# RewriteCond %{SERVER_NAME} =www.example.com [OR]
# RewriteCond %{SERVER_NAME} =www.*.example.com
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]