gpt4 book ai didi

php - 在 Apache 上强制使用 HTTPS 并产生奇怪的结果

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:11 25 4
gpt4 key购买 nike

我刚刚为我的网站购买了 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]

当我转到 example.com 时,它会转到安全页面。太棒了!

.htaccess (B)

我的 public_html 文件有这些规则

<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]

问题:

如果我去http://www.example.com/login ,

而不是重定向到https://www.example.com/login ,

它重定向到https://www.example.com/index.php

最佳答案

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

如果您只是将 https-check/redirect 移动到其他规则之前,它应该可以工作。

<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>

我还删除了第二个 RewriteEngine On。拥有一次就够了。

关于php - 在 Apache 上强制使用 HTTPS 并产生奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44617650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com