htaccess 路由 http 到 https 有时包括删除的 CodeIgniter 'index.php'。为什么?



我有以下 mod 重写规则 - 第一个从 URI 请求中删除"index.php"段,第二个强制 HTTPS 连接。但是,第二个是重新插入"索引.php"。

# send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# force SSL
RewriteCond %{HTTP_HOST}  .
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

例如-

http://www.mysite.tld/somepage

成为

https://www.mysite.tld/index.php?/somepage

我仍在尝试了解如何编写htaccess文件,但是有没有办法将这两者结合起来以避免此问题?

您需要交换规则的顺序。

# force SSL
RewriteCond %{HTTP_HOST}  .
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

当重定向规则最后一次时,它仍在应用,因为应用了第一个规则,重写停止了该迭代,然后重写引擎循环,然后应用第二个规则,URI(现在包含一个index.php)被重定向。

最新更新