【问题标题】:Rewriterule remove index.php from url for codeigniter causes too many redirectRewriterule 从 url 中删除 index.php for codeigniter 导致重定向过多
【发布时间】:2020-11-17 21:15:11
【问题描述】:

以前由于不正确的 .htaccess 逻辑,我的许多 http://example.com/path/ url 被重定向到 https://example.com/index.php?/path/

所以有很多页面被谷歌抓取到https://example.com/index.php?/path/

我正在使用 codeigniter,我不想在我的 URL 中使用 index.php,所以我有从 URL 中删除 index.php 的重写规则,所以下面是重写规则。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

但是由于 Google 已经在 URL 中使用 index.php 抓取了许多页面,我想将这些 URL 重定向到没有 index.php 但这会产生冲突并创建无限重定向,一个删除然后一个添加,然后开。

RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)index\.php https://example.com%1? [R=302,L]

所以当我访问 example.com/path/ 时,这会导致“重定向过多”问题。

我的逻辑在这里卡住了。

我该怎么办?

【问题讨论】:

  • 你把它放在哪里,在现有的重写之上还是之下? (你到底想在index.php 之前与(.*) 匹配什么?)
  • @CBroe 嗨,我已经把现有的放在下面,但我尝试移到顶部,没有区别。对于 (.*),最初我以为可能有一些 url example.com/path/index.php?/path2/ 但是现在我总是在一开始就意识到,我猜 index.php 之前的 (.*) 是不必要的。

标签: apache .htaccess codeigniter mod-rewrite url-rewriting


【解决方案1】:

好的,我在这里找到了同样问题的答案,为了避免重定向循环,关键是使用

RewriteCond %{ENV:REDIRECT_STATUS} ^$

Remove and Redirect index.php from CodeIgniter

【讨论】:

    猜你喜欢
    • 2015-03-19
    • 2020-05-26
    • 1970-01-01
    • 2016-05-14
    • 2014-10-28
    • 2012-06-15
    • 2012-09-21
    相关资源
    最近更新 更多