title: HTTP to HTTPS and 301 redirect chains on Magento
body: <p>We have successfully moved from <code>HTTP</code> to <code>HTTPS</code>, and now got some redirect chains. We want to do the following, directly:</p>
  
  <ol>
  <li><p>From http ://domain.tld to https ://www.domain.tld</p></li>
  <li><p>From http ://domain.tld/subdir/ to https ://www.domain.tld/subdir/</p></li>
  </ol>
  
  <p>Right now, www is added first, then again <code>301</code> to <code>HTTPS</code>, like this:</p>
  
  <ol>
  <li><p>http ://domain.tld => http ://www.domain.tld => https ://www.domain.tld</p></li>
  <li><p>http ://domain.tld/subdir/ => http ://www.domain.tld/subdir/ => https ://www.domain.tld/subdir/</p></li>
  </ol>
  
  <p>Also, if possible, all the other domains on the server (Magento stores), should not be able to use HTTPS, and should be redirected back to HTTPS only.</p>
  
  <p>Thanks,</p>
  
  <pre><code>DirectoryIndex index.php
  SetEnvIf SERVER_PORT 443 HTTPS=on
  SetEnvIf X-Forwarded-Proto https HTTPS=on
  
  &lt;IfModule mod_rewrite.c&gt;
  
  // DEFAULT //  
  RewriteEngine on
  Options +FollowSymLinks 
  RewriteBase /
  DirectoryIndex index.php
  
  
  // REDIRECT ALL TO HTTPS //
  RewriteCond %{HTTP_HOST} www\.domain\.tld [NC]
  RewriteCond %{HTTPS} !=on
  RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L] 
  
  // REMOVE INDEX.PHP AND REIDIRECT TO ROOT DIRECTORY //  
  # IN ALL DIRECTORIES (EVEN IN SUB DIRECTORIES)
  # RewriteCond %{THE_REQUEST} /index\.php [NC]
  # ONLY WHEN ON ROOT
  RewriteCond %{THE_REQUEST} \s+/index\.php [NC]
  RewriteRule ^(.*?)index\.php$ /$1? [L,R=301,NC,NE]
  
  // REMOVE HOME REWRITE FROM MAGENTO //  
  RewriteRule ^home/?$ /? [R=301,L,NC]
  
  // ADD WWW TO NONE WWW FOR BOTH HTTPS AND NONE HTTPS // 
  RewriteCond %{HTTP_HOST} !^$
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteCond %{HTTPS}s ^on(s)|
  RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  
  // REDIRECT ALL .HTML FILES AND ALL .HTML/ FILES WITH TRAILING SLASH // 
  RewriteRule ^google[0-9a-f]+.html$ - [L]
  RewriteRule (.+)\.html$ /$1/ [L,R=301]
  RewriteRule (.+)\.html\/$ /$1/ [L,R=301]
  
  // ADD TRAILING SLASH //
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteCond %{REQUEST_URI} !(.*)/$
  RewriteRule ^(.*)$ $1/ [L,R=301]
  
  // CHECK IF REDIRECT POINTS TO A VALID FILE ##
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  
  // REWRITE EVERYTHING ELSE TO INDEX.PHP //
  RewriteRule .* index.php [L]
  
  &lt;/IfModule&gt;
  </code></pre>