apache - htaccess 使用 index.php 重定向到 https

标签 apache .htaccess codeigniter mod-rewrite

嘿,我正在使用 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/

相关文章:

apache - 在 WAMP 服务器上安装 SSL - 有时只能通过 HTTPS 连接

php - 加载集中的 HTML 和 CSS 文件

javascript - 模板上动态添加的内容不响应 CodeIgniter 中的 Javascript/AJAX/Jquery

python - Django 错误页面不显示语法错误

apache - 坚果索引元数据未索引

javascript - 阻止了原点的帧如何修复

php - Heroku 403 禁止错误

php - REST 请求中的特殊字符

php - Codeigniter - 从许多表中获取数据

apache - 将空格、%20 和 + 替换为连字符