title: Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)
body: <p>I have been searching for the perfect 301 redirect. But I am finding so many solutions and do not know what’s best.</p>
  
  <p><strong>Here is what I want to do</strong></p>
  
  <ul>
  <li><a href="http://domain.tld/" rel="noreferrer">http://domain.tld/</a> → <a href="https://domain.tld/" rel="noreferrer">https://domain.tld/</a></li>
  <li><a href="http://www.domain.tld/" rel="noreferrer">http://www.domain.tld/</a> → <a href="https://domain.tld/" rel="noreferrer">https://domain.tld/</a></li>
  <li><a href="https://www.domain.tld/" rel="noreferrer">https://www.domain.tld/</a> → <a href="https://domain.tld/" rel="noreferrer">https://domain.tld/</a></li>
  </ul>
  
  <p><strong>Best practice .htacess?</strong></p>
  
  <pre><code>&lt;IfModule mod_rewrite.c&gt;
      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
      RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
  &lt;/IfModule&gt;
  </code></pre>
  
  <p>This is my preferred code. At least unil now.</p>
  
  <p><strong>Alternative ways</strong></p>
  
  <p>I also found a lot of other ways to redirect from <code>HTTP</code> to <code>HTTPS</code>. For example:</p>
  
  <p><strong>1.</strong></p>
  
  <pre><code>RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
  </code></pre>
  
  <p>Missing one step? And no <code>[R=301,L]</code> here?</p>
  
  <p><strong>2.</strong></p>
  
  <pre><code>RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/13977851/htaccess-redirect-to-https-www">Is a different order generally better</a>?</p>
  
  <p>Should I use</p>
  
  <pre><code>RewriteRule ^(.*)$
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>RewriteRule (.*)
  </code></pre>
  
  <p>?</p>
  
  <p><strong>3.</strong></p>
  
  <pre><code>RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} example\.com$ [NC]
  RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
  </code></pre>
  
  <p>Does using the full domain name have any performance advantages? Do I really need <code>NE</code>? (<code>[R=301,L,NE]</code> vs. <code>[L,R=301]</code>)</p>
  
  <p>So, my question to all experts: What's the best (performing) way to redirect both from <code>HTTP</code> to <code>HTTPS</code> and from <code></code> to <code>HTTPS://</code> ?</p>