Magento上的HTTP到HTTPS和301重定向链

时间:2014-09-01 06:47:24

标签: apache .htaccess magento redirect

我们已成功从HTTP转移到HTTPS,现在有了一些重定向链。我们希望直接执行以下操作:

  1. 从http://domain.tld到https://www.domain.tld

  2. 从http://domain.tld/subdir/到https://www.domain.tld/subdir/

  3. 现在,首先添加www,然后再添加301HTTPS,如下所示:

    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/

    3. 此外,如果可能,服务器上的所有其他域(Magento商店)都不应该使用HTTPS,并且应该仅重定向回HTTPS。

      谢谢,

      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>
      

1 个答案:

答案 0 :(得分:1)

您可以将http->httpsnon-www->www规则合并到一个301重定向规则中,如下所示:

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