嘿,我正在使用 codeigniter 并添加了重写规则以从工作正常的 url 中删除 index.php
。
但是,我添加了一条规则,将所有 http 请求重定向到 https,但当它重定向时,它会使用 index.php 重定向。
意思是如果我输入这个 url :domain.com/somecoolcontroler
它会将我重定向到:https://domain.com/index.php/somecoolcontroler
但是当之后导航时,它会在没有它的情况下返回正常的 url,所以我猜问题出在重定向规则中,这就是我在 htaccess 中输入的内容:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
我该如何解决?
最佳答案
您需要将重定向规则放在之前您的路由规则。重写引擎循环,所以即使你有 L
标志,它也会第二次循环通过你的路由规则并命中重定向规则,除了这次,URI 已经被重写。
如果重写规则是第一个,它会在应用路由规则之前重定向浏览器。然后,当发出重定向请求时,将应用路由规则。您还需要一个 L
标志。
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
关于apache - htaccess 使用 index.php 重定向到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14028954/