title: Redirecting www to non-www while maintaining the protocol HTTP or HTTPS
body: <p>I'm attempting to redirect www to non-www for both HTTP and HTTPS requests. My root .htaccess looks like this:</p>
  
  <pre><code>RewriteEngine on
  
  RewriteCond %{HTTP_HOST} ^www.example.com$
  RewriteRule ^(.*)$ http://example.com/$1 [R=301]
  
  RewriteCond %{HTTP_HOST} ^www.example.com$
  RewriteCond %{SERVER_PORT} ^443
  RewriteRule ^(.*)$ https://example.com/$1 [R=301]
  </code></pre>
  
  <p>This isn't fully working as expected. What happens:</p>
  
  <p>Visiting <code>http://www.example.com</code> results in a redirect to <code>http://example.com</code>. This indicates my first rule and condition are working, the mod_rewite module is hunky-dory and .htaccess is enabled OK.</p>
  
  <p>Visiting <code>https://www.example.com</code> doesn't result in a redirect. I remain on <code>https://www.example.com</code></p>
  
  <p><strong>My question</strong></p>
  
  <p>In order for the above rewrite rules to work, must my server have an SSL certificate? It currently doesn't and I'm wondering if that is the reason things aren't working.</p>