【问题标题】:force HTTPS only on non www仅在非 www 上强制 HTTPS
【发布时间】:2018-10-01 19:47:18
【问题描述】:

当前代码

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.test.com$ [OR]
RewriteCond %{HTTP_HOST} ^test.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

我需要将非 www 流量重定向到 HTTPS www,但需要保留直接访问的 www 流量而不强制执行 HTTPS。

所以我尝试了这个

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.test.com$ [OR]
RewriteCond %{HTTP_HOST} ^test.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

但我收到“太多重定向”,不知道为什么。 index.php 来自 laravel 安装。

【问题讨论】:

    标签: laravel apache .htaccess url-rewriting


    【解决方案1】:

    假设您有 2 个虚拟主机,我正在使用您的重定向规则:

    Listen 80
    <VirtualHost *:80>
        ServerName www.test.com
        ServerAlias test.com
    
        [... OTHER CONFIGURATION ...]
    
        RewriteEngine On
    
        RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
        RewriteCond %{HTTPS} off
        RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]
    
        RewriteCond %{HTTP_HOST} ^www.test.com$ [OR]
        RewriteCond %{HTTP_HOST} ^test.com$
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]        
    </VirtualHost
    
    Listen 443
    <VirtualHost *:443>
        ServerName www.test.com
        ServerAlias test.com
    
        [... OTHER CONFIGURATION ...]        
    </VirtualHost>
    

    你要求http://test.com --> https://test.com/$1

    because of `RewriteCond %{HTTP_HOST} ^test\.com$`
    

    你要求http://ANYTHING --> https://test.com/$1

    因为RewriteCond %{HTTPS} off。为什么?因为[OR] 选项。它应该是一个隐含的“[AND]”。所以删除[OR]。第二个也是一样。

    另外,您在第二组指令中自相矛盾。它说:

    • 是 www.test.com
    • 或 test.com
    • 不是目录
    • 不是文件
    • 转到 index.php

    您说过要将非 www 请求重定向到 https。

    所以你想要:

    此配置将执行此操作:

    Listen 80
    <VirtualHost *:80>
        ServerName www.test.com
        ServerAlias test.com
    
        [... OTHER CONFIGURATION ...]
    
        RewriteEngine On
    
        # http://test.com --> redirect to <VirtualHost *:443>
        # Everything else stays in <VirtualHost *:80>
        RewriteCond %{HTTP_HOST} ^test\.com$
        RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]
    
        # Default page index.php, avoid 404
        RewriteCond %{HTTP_HOST} ^www.test.com$
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </VirtualHost
    
    Listen 443
    <VirtualHost *:443>
        ServerName www.test.com
        ServerAlias test.com
    
        [... OTHER CONFIGURATION ...]        
    
        # Default page index.php, avoid 404
        RewriteCond %{HTTP_HOST} ^www.test.com$ [OR]
        RewriteCond %{HTTP_HOST} ^test.com$
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </VirtualHost>
    

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 2015-05-10
      • 1970-01-01
      • 2020-09-26
      • 2018-08-20
      • 2014-02-20
      相关资源
      最近更新 更多