【问题标题】:.htaccess - SSL redicect condition for one specific domain not working.htaccess - 一个特定域的 SSL redicect 条件不起作用
【发布时间】:2018-11-25 04:40:18
【问题描述】:

我有一个托管多个网站的 apache 服务器:

main-website.com 
subdomain1.main-website.com    
subdomain2.main-website.com 
another-website1.com 
another-website2.com    
another-website3.com

我只需要将https://www.main-website.com 重定向到https://www.main-website.com

子域和所有其他网站不需要 ssl 证书;因此我想通过指定只需要重定向主网站来将它们排除在重定向之外。

这是我的 .htaccess 语法(在这个主题上进行了大量研究似乎是正确的)

#NON-WWW to WWW (whis applies to all domains and subdomains)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#redirect HTTP to HTTPS only for main-website.com:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} main-website.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

指定了重定向main-website.com的条件,但是不起作用!事实上,所有其他域和子域都被重定向到 HTTPS!

你知道错误可能在哪里吗?

谢谢你:)

【问题讨论】:

    标签: apache .htaccess ssl redirect server


    【解决方案1】:

    你的问题一定有错别字,你想从https://www.main-website.com重定向到https://www.main-website.com我理所当然地认为你想说http://www.main-website.comhttps://www.main-website.com

    所以,你的第一个 RewriteCond-RewriteRule 集合重写:

    somesite.com   --->   https://www.somesite.com
    

    但请注意,您还强制使用 https。因此,任何到达http://somesite.com 的尝试都将转到https://www.somesite.com

    可能是您的其他域触发了此规则集。第一条规则应该是重写:non wwwwww,此时不强制 https。

    那么下一个规则集将仅适用于您的主网站并将其发送到 https。

    【讨论】:

      【解决方案2】:
      #NON-WWW to WWW (whis applies to all domains and subdomains)
      RewriteCond %{HTTP_HOST} !^www\.
      RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      
      #redirect HTTP to HTTPS only for main-website.com:
      RewriteCond %{HTTPS} off
      RewriteCond %{HTTP_HOST} main-website.com [NC]
      RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      

      我已经更新了第一组说明。不过还是不行:(

      【讨论】:

      • 不要这样做作为答案,使用所有相关数据更新您的帖子。并且停止做无用的虚假混淆,至少使用 RFC2606 中的名称。
      猜你喜欢
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多