【问题标题】:I can't redirect magento domain to HTTPS and add www我无法将 magento 域重定向到 HTTPS 并添加 www
【发布时间】:2016-02-01 03:48:00
【问题描述】:

我正在尝试将 magento 从 any.example 重定向到 https://www.example.com

ht访问代码:

<IfModule mod_rewrite.c>

############################################
## enable rewrites

    Options +FollowSymLinks
    RewriteEngine on

############################################
## you can put here your magento root folder
## path relative to web root

    #RewriteBase /magento/

############################################
## uncomment next line to enable light API calls processing

#    RewriteRule ^api/([a-z][0-9a-z_]+)/?$ api.php?type=$1 [QSA,L]

############################################
## rewrite API2 calls to api.php (by now it is REST only)

    RewriteRule ^api/rest api.php?type=rest [QSA,L]

############################################
## workaround for HTTP authorization
## in CGI environment

    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

############################################
## TRACE and TRACK HTTP methods disabled to prevent XSS attacks

    RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
    RewriteRule .* - [L,R=405]

############################################
## redirect for mobile user agents

    #RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
    #RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
    #RewriteRule ^(.*)$ /mobiledirectoryhere/ [L,R=302]

############################################
## always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]


</IfModule>

【问题讨论】:

    标签: php .htaccess magento mod-rewrite


    【解决方案1】:

    以下重写规则应将所有非 https 请求通过 301 重定向的 https 转发到相应页面。

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.example.com [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
    

    也可以参考下面的链接

    https://wiki.apache.org/httpd/RewriteHTTPToHTTPS

    【讨论】:

    • 这个解决方案出现了这个错误 ERR_TOO_MANY_REDIRECTS
    • 你也可以在admin->catalog->URL rewrite Management中使用URL rewrite rule
    【解决方案2】:

    确保您的主题没有对 http 的引用

    然后在你的 htaccess 文件中添加/替换这个,

    ############################################
    ## enable rewrites
    
        Options +FollowSymLinks
        RewriteEngine on
    
    ############################################
    ## REDIRECT TO HTTPS ALWAYS
        RewriteCond %{SERVER_PORT} !443
        RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    

    【讨论】:

      【解决方案3】:

      如果我正确理解了您的目标,那么您的第一个 Cond 模式的否定似乎是不正确的(您的否定前瞻已经排除了 www)。试试这个:

      RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
      

      我也会整理规则本身:

      RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
      

      更新所有更正:

      RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC,OR]
      RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
      RewriteCond %{HTTPS} off
      RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
      

      【讨论】:

      • 另外,第二个 RewriteCond 似乎与您的第一个相矛盾。您是第一个要求域前缀...即“something.example.com”,但您是第二个仅与“example.com”匹配。也许 OR 标志应该在你的第一个 RewriteCond 而不是第二个?用所有更正更新了我的答案。
      • 此解决方案没有重定向我的域...它避免了 ERR_TOO_MANY_REDIRECTS 消息但不起作用...
      • 确保您的重写模块在主配置中启用,并且没有其他冲突的重写。假设这些已经过验证,那么:您正在测试的完整 URL 是什么?您希望重定向到的完整 URL 是什么?你得到的完整 URL 是什么?
      【解决方案4】:

      这个解决方案效果很好......

      RewriteCond %{HTTP:X-Forwarded-SSL} !on
      RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      

      【讨论】:

      • 不确定如何处理“添加 www”域要求,但很高兴您找到了解决方案。
      • Magento 有一个 url 选项叫做:Auto-redirect to Base URL;
      猜你喜欢
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 2018-06-01
      • 2018-06-02
      • 2015-05-19
      • 2019-11-15
      • 2019-02-21
      • 2017-04-22
      相关资源
      最近更新 更多