title: How to always redirect to https://www and https://subdomain?
body: <p>I want to redirect everything to URLs that start with <code>https://www</code> or <code>https://subdomain</code>. If it's a URL with no subdomain then I want it to always redirect to the <code>https://www</code> version. And if there is a non-www subdomain then I want it to always redirect to the <code>https://subdomain</code> version.</p>
  
  <p>The first thing I had would just redirect everything to <code>https://www</code>. Here's that:</p>
  
  <pre>RewriteCond %{HTTPS} off
  RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]</pre>
  
  <p>I think the only problem with that is that it will redirect non-www subdomains to the multilevel <code>https://www.subdomain</code>. So then I added a second condition to the second rule:</p>
  
  <pre>RewriteCond %{HTTPS} off
  RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
  RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]</pre>
  
  <p>This appears to work for everything I want with one problem. If there is a subdomain and something after the domain like a directory then it won't work. For example this:</p>
  
  <p><code>sub.site.com</code></p>
  
  <p>will correctly redirect to:</p>
  
  <p><code>https://sub.site.com</code></p>
  
  <p>But this:</p>
  
  <p><code>sub.site.com/test/</code></p>
  
  <p>will not redirect to:</p>
  
  <p><code>https://sub.site.com/test/</code></p>
  
  <p>So why doesn't this last URL get rewritten when there is something after the domain like the /test/ directory?</p>