简体   繁体   中英

HTTP to HTTPS and 301 redirect chains on Magento

We have successfully moved from HTTP to HTTPS , and now got some redirect chains. We want to do the following, directly:

  1. From http ://domain.tld to https ://www.domain.tld

  2. From http ://domain.tld/subdir/ to https ://www.domain.tld/subdir/

Right now, www is added first, then again 301 to HTTPS , like this:

  1. http ://domain.tld => http ://www.domain.tld => https ://www.domain.tld

  2. http ://domain.tld/subdir/ => http ://www.domain.tld/subdir/ => https ://www.domain.tld/subdir/

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.

Thanks,

DirectoryIndex index.php
SetEnvIf SERVER_PORT 443 HTTPS=on
SetEnvIf X-Forwarded-Proto https HTTPS=on

<IfModule mod_rewrite.c>

// 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]

</IfModule>

You can combine both http->https and non-www->www rules in one single 301 redirect rule like this:

# // REDIRECT main domain to HTTPS and add www //
RewriteCond %{HTTP_HOST} !^(?!www\.)[^.]+\.domain\.tld$ [NC]
RewriteCond %{HTTP_HOST} ^domain\.tld$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.tld%{REQUEST_URI} [R=301,L,NE]

# redirect sub domains to non http
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.domain\.tld$ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM