Php 在Apache上强制使用HTTPS会产生奇怪的结果

Php 在Apache上强制使用HTTPS会产生奇怪的结果,php,apache,.htaccess,ssl,mod-rewrite,Php,Apache,.htaccess,Ssl,Mod Rewrite,我刚刚为我的网站购买了一个SSL证书,我正试图强制使用HTTPS。它有点管用,但不是真的管用。我在Apache上使用PHP Slim框架进行自定义路由 我的文件夹结构 root/ .htaccess (A) php/ public_html/ .htaccess (B) .htaccess(A) 我的主htaccess文件有以下规则 RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ h

我刚刚为我的网站购买了一个SSL证书,我正试图强制使用HTTPS。它有点管用,但不是真的管用。我在Apache上使用PHP Slim框架进行自定义路由

我的文件夹结构

root/
   .htaccess (A)
   php/
   public_html/
     .htaccess (B)
.htaccess(A)

我的主htaccess文件有以下规则

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
    RewriteEngine on
    #RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [QSA,L]
</IfModule>


# Always use https for secure connections
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
当我进入
example.com
时,它会进入安全页面。太好了

.htaccess(B)

我的公共html文件有以下规则

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
    RewriteEngine on
    #RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [QSA,L]
</IfModule>


# Always use https for secure connections
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

重新启动发动机
#重写基/
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写规则。index.php[QSA,L]
#始终使用https进行安全连接
重新启动发动机
重写cond%{SERVER_PORT}80
重写规则^(.*)$https://www.example.com/$1[R=301,L]
问题:

如果我去

而不是重定向到


它从上到下重定向到htaccess文件中的规则。在htaccess中,它首先检查服务器上是否存在请求的资源(/login)。如果没有,它会将请求重定向到index.php

如果您只是将https检查/重定向移动到另一个规则之前,那么它应该可以工作

<IfModule mod_rewrite.c>
    RewriteEngine on
    #RewriteBase /

    # Always use https for secure connections
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

    # Redirect requests to non existing resources to index.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [QSA,L]
</IfModule>

重新启动发动机
#重写基/
#始终使用https进行安全连接
重写cond%{SERVER_PORT}80
重写规则^(.*)$https://www.example.com/$1[R=301,L]
#将对不存在资源的请求重定向到index.php
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写规则。index.php[QSA,L]

我还删除了上的第二个
重写引擎。拥有一次就足够了。

htaccess文件中的规则将从上到下进行验证。在htaccess中,它首先检查服务器上是否存在请求的资源(/login)。如果没有,它会将请求重定向到index.php

如果您只是将https检查/重定向移动到另一个规则之前,那么它应该可以工作

<IfModule mod_rewrite.c>
    RewriteEngine on
    #RewriteBase /

    # Always use https for secure connections
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

    # Redirect requests to non existing resources to index.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [QSA,L]
</IfModule>

重新启动发动机
#重写基/
#始终使用https进行安全连接
重写cond%{SERVER_PORT}80
重写规则^(.*)$https://www.example.com/$1[R=301,L]
#将对不存在资源的请求重定向到index.php
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写规则。index.php[QSA,L]

我还删除了
上的第二个
重写引擎。拥有一次就足够了。

这是因为在htaccess中有两套规则。在
中的第一条规则检查请求的文件是否存在,如果不存在,则重定向到
index.php
。由于
/login
可能不作为真实文件存在,因此将触发该规则。只需将您的https检查移动到另一个规则之前。将其添加为答案,以便您可以接受它并关闭问题。这是因为您的htaccess中有两组规则。在
中的第一条规则检查请求的文件是否存在,如果不存在,则重定向到
index.php
。由于
/login
可能不作为真实文件存在,因此将触发该规则。只需将您的https检查移动到其他规则之前。将其添加为答案,以便您可以接受它并关闭问题。