【发布时间】:2017-11-20 21:47:03
【问题描述】:
我刚刚为我的网站购买了 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,
【问题讨论】:
-
那是因为您的 htaccess 中有 两 组规则。
<IfModule mod_rewrite.c>中的第一条规则检查请求的文件是否存在,如果不存在,则重定向到index.php。由于/login可能不作为真实文件存在,因此将触发该规则。只需将您的 https-check 移到另一条规则之前。 -
已将其添加为答案,因此您可以接受并关闭问题。
标签: php apache .htaccess ssl mod-rewrite