title: Apache SSL Rewrite with Wildcard Subdomains
body: <p>I'm attempting to setup an SSL redirect in Apache using RewriteEngine that will do the following:</p>
  
  <ul>
  <li>Redirect traffic to either <a href="http://mydomain.com" rel="nofollow">http://mydomain.com</a> or <a href="http://www.mydomain.com" rel="nofollow">http://www.mydomain.com</a> to use HTTPS</li>
  <li>Redirect traffic to any other subdomain https://*.mydomain.com to use HTTP instead of HTTPS</li>
  </ul>
  
  <p>My reasoning for this is that I'm developing a project that's using a free SSL certificate until launch. This certificate covers the base domain, but none of the wildcard subdomains, and it's a pain to need to bypass the warning every time I visit one of the subdomains.</p>
  
  <p>Edit:
  I believe I'm close here, but I still can't get the HTTPS to HTTP redirect to work properly.</p>
  
  <pre><code>RewriteEngine on
  # Redirect domain and www to HTTPS
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} =mydomain.com [or]
  RewriteCond %{HTTP_HOST} =www.mydomain.com
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  # Redirect wildcard subdomains to HTTP
  RewriteCond %{HTTPS} on
  RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
  RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  </code></pre>