【问题标题】:Apache2 redirect to another domain with sslApache2 使用 ssl 重定向到另一个域
【发布时间】:2016-05-11 02:58:27
【问题描述】:

我不知道为什么会失败。我只想将所有域重定向到www.maindomain.comhttphttps,我错过了什么?

# redirect http to https    
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# redirect without www to www
RewriteCond %{http_host} ^maindomain.com [nc]
RewriteRule ^(.*)$ https://www.maindomain.com  [r=301,nc]

# redirect another domain to www.maindomain.com    
RewriteCond %{HTTPS} off # this i was missing 
RewriteCond %{HTTP_HOST} ^(www\.)?anotherdomain.com [NC]
RewriteRule ^(.*)$ https://www.maindomain.com [R=301,L]
  1. http://maindomain.comhttps:/www.maindomain.com/ 有效
  2. http://anotherdomain.comhttps:/www.maindomain.com/ 有效
  3. https://anotherdomain.comhttps:/www.maindomain.com/ 失败

【问题讨论】:

  • 奇怪,RewriteCond %{HTTPS} 关闭的评论,提示现在正在工作,但答案已被删除,就像我想接受它一样,我错过了第二个 RewriteCond第三次重定向

标签: apache .htaccess redirect ssl


【解决方案1】:

另一个域的 Http 到 Https 重定向失败,因为您的规则缺少以下行:

RewriteCond %{HTTPS} off

试试:

# redirect another domain to www.maindomain.com    

RewriteCond %{HTTPS} off

RewriteCond %{HTTP_HOST} ^(www\.)?anotherdomain.com [NC]
RewriteRule ^(.*)$ https://www.maindomain.com [R=301,L]

【讨论】:

    【解决方案2】:

    你可以试试这个:

    RewriteEngine On
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    

    只需将上述代码复制并粘贴到 .htaccess 文件中,当浏览器以“http”模式打开时,整个网站将被重定向到“https”。浏览器只是使用 .htaccess 中的 url 重写来重定向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      • 2016-08-13
      相关资源
      最近更新 更多