title: htaccess force https and redirect www to non-www, but no other subdomains
body: <p>I know there are many similar threads, but none of them seems to match my exact problem. Here is what I’m trying to do:</p>
  
  <pre><code>(1) http://www.mydomain.com/ -&gt; https://mydomain.com/
  (2) http://mydomain.com/     -&gt; https://mydomain.com/
  (3) https://www.mydomain.com -&gt; https://mydomain.com/
  </code></pre>
  
  <p>Ideally, I would also like to cover the situation if I will ever add more subdomains, to automatically behave like the following (preferably in a generic way that will work for any subdomain I add).</p>
  
  <pre><code>(4) http://sub.mydomain.com/ -&gt; https://sub.mydomain.com/
  </code></pre>
  
  <p>At this point I’m wondering if it is even possible to create a single .htaccess file that does everything I need, although I have to admit that I understand regexes, but I’m not exactly a mod_rewrite uberpro.</p>
  
  <p>Here are the solutions that I already tried:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/6347183/force-non-www-and-https-via-htaccess">Force non-www and https via htaccess</a>
  
  <ul>
  <li>Sort of works, but seems to generate a redirect loop</li>
  </ul></li>
  <li><a href="https://stackoverflow.com/questions/1478173/htaccess-redirect-www-to-non-www-with-ssl-https">.htaccess redirect www to non-www with SSL/HTTPS</a>
  
  <ul>
  <li>Works for (1) and (3), but not for (2)</li>
  </ul></li>
  <li><a href="https://stackoverflow.com/questions/17846827/htaccess-force-www-to-non-www-with-consideration-of-http-or-https">htaccess force www to non-www with consideration of http or https</a>
  
  <ul>
  <li>Redirects to <a href="http://mydomain.com/" rel="noreferrer">http://mydomain.com/</a> (without https) for (1)</li>
  <li>Does nothing for (2)</li>
  <li>Redirects to <a href="http://mydomain.com/" rel="noreferrer">http://mydomain.com/</a> (without https) for (3)</li>
  </ul></li>
  </ul>
  
  <p>My SSL certificate covers the www-subdomain, so I’m not looking for a 'untrusted connection' error solution, I’m aware that isn’t possible.</p>