title: Redirect all non-www to www for both http and https with .htaccess
body: <p>I want to redirect all non-www to www - if the request is through http it should redirect to <a href="http://www.domain.com" rel="nofollow">http://www.domain.com</a>, if the request is through https then direct to <a href="https://www.domain.com" rel="nofollow">https://www.domain.com</a></p>
  
  <p>I have tried the following in my .htaccess, but it redirects everything to https</p>
  
  <pre><code>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
  </code></pre>
  
  <hr>
  
  <p>I have used this code and it's resolving perfectly. Please check whether it's correct or not.</p>
  
  <pre><code>RewriteEngine On
  RewriteBase /
  #Redirect non-www to www
  
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
  
  # Redirect to HTTPS
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://www.pnrstatusbuzz.in/%{REQUEST_URI}
  </code></pre>