【问题标题】:htaccess routing http to https sometimes includes removed CodeIgniter 'index.php'. Why?htaccess 路由 http 到 https 有时包括删除的 CodeIgniter 'index.php'。为什么?
【发布时间】:2013-09-22 10:13:03
【问题描述】:

我有以下 mod 重写规则 - 第一个从 URI 请求中删除“index.php”段,第二个强制 HTTPS 连接。然而第二个是重新插入'index.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 文件,但有没有办法将这两者结合起来避免这个问题?

【问题讨论】:

    标签: .htaccess codeigniter mod-rewrite https


    【解决方案1】:

    您需要交换规则的顺序。

    # 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(现在包含 @987654322 @) 被重定向。

    【讨论】:

    • 我试过了,但它停止强制使用 HTTPS。我明白你的逻辑,所以我不太确定为什么!
    • 恐怕即使有这些标志,它也不会强制使用 HTTPS。任何其他想法为什么?
    • @dieselpower44 不知道,在我的测试安装中工作得非常好,带有一个空白的 htaccess 文件。
    • 肯定 {REQUEST_FILENAME} 仍然与初始请求相同?因此,即使我们将其更改为 https://,该变量仍然是 http://?还是 {REQUEST_FILENAME} 变量实际上被重写改变了?
    • @dieselpower44 %{REQUEST_FILENAME}不管是http还是https,都是apache认为URI当前映射到的文件/目录
    猜你喜欢
    • 1970-01-01
    • 2019-04-06
    • 2016-03-15
    • 2013-04-13
    • 1970-01-01
    • 2016-05-13
    • 2016-05-17
    • 1970-01-01
    • 2015-03-09
    相关资源
    最近更新 更多