title: Redirecting *.domain.com to www.domain.com with HTTP or HTTPS prefix
body: <p>The code below is a redirecting script that redirects visitors of my website to www.domain.com if they are not on it (like for example if they are on domain.com or somesub.domain.com). I modified it, to redirect them to either HTTPS or HTTP, depending on the link that they typed or got (hot)linked to.</p>
  
  <p>It is working, but i'm guessing there could be an easier way to do this or that there is some room for improvement. Can someone double check / comment on this? Many thanks in advance.</p>
  
  <pre><code># redirect any HTTP traffic that is not http://www.domain.com/*
  RewriteEngine On
  RewriteCond %{SERVER_PORT} ^80$
  RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
  RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
  
  # redirect any HTTPS traffic that is not https://www.domain.com/*  
  RewriteEngine On
  RewriteCond %{SERVER_PORT} ^443$
  RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
  RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
  </code></pre>