【问题标题】:Code Igniter Redirect Issues with GET parametersGET 参数的代码点火器重定向问题
【发布时间】:2011-03-19 21:51:45
【问题描述】:

我在使用代码点火器路由时遇到了困难。

http://www.mysite.com 找到正确的控制器并做正确的事。但是,http://www.mysite.com/?ref=p&t2=455 会导致 404 错误。 http://www.mysite.com/mycontroller/mymethod/?ref=p&t2=455 也可以正常工作。

我更改了 config.php 文件中的 uri_protocol 并尝试了不同的值。自动似乎效果最好。

我的理论是代码点火器正在使用查询参数来进行路由。问题是这些查询参数与路由无关。

如何告诉代码点火器忽略默认控制器的查询参数?

注意,我按照在线说明从 URL 中删除了 index.php。我不认为它会导致问题,但这是我的 .htaccess 文件以防万一:

RewriteEngine On
RewriteBase /~trifecta/prod/

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php?/$1 [L]

【问题讨论】:

  • RewriteRule 行中删除问号,除非您使用的是疯狂的 CGI 主机或其他东西。

标签: codeigniter redirect get http-status-code-404


【解决方案1】:

使用重写规则RewriteRule ^(.*)$ /index.php?/$1,以下是一些正在发生的重写示例:

http://www.mysite.com =>
http://www.mysite.com/index.php?/(实际上可能根本不会重写)

http://www.mysite.com/mycontroller/mymethod/?ref=p&t2=455 =>
http://www.mysite.com/index.php?/mycontroller/mymethod/?ref=p&t2=455

http://www.mysite.com/?ref=p&ts=455 =>
http://www.mysite.com/index.php?/?ref=p&t2=455

无论是否重写,第一个都可以使用。 CodeIgniter 正在处理一个空查询字符串(这很容易)或一个简单的“/”查询字符串。

第二个(也可以工作)正在重写,但 CodeIgniter 能够处理其查询字符串,即/mycontroller/mymethod/?ref=p&t2=455。 CI 把它变成一个段数组

[0] => mycontroller
[1] => mymethod
[2] => ?ref=p&t2=455

数组索引 2 最终会被您所做的任何事情忽略。

第三个(不起作用工作正在被重写,CodeIgniter 根本无法处理它的查询字符串。它的查询字符串被重写为:/?ref=p&t2=455。这构成了一个数组看起来像这样的片段:

[0] => ?ref=p&t2=455

与您网站上的任何控制器都不匹配。

您可能会通过将 RewriteRule 从
RewriteRule ^(.*)$ /index.php?/$1 修改为
RewriteRule ^(.*)$ /index.php/$1
来解决整个问题 此时您可能希望将uri_protocol 配置更改回PATH_INFO

【讨论】:

  • 我完全同意。重写规则看起来有点时髦。
  • 一些 场合您需要在查询字符串中传递所有内容,但在传递另一个查询字符串时,这将永远无法正常工作。如果查询字符串仅在客户端使用,您可以通过放置一个 ? 来消除重写中的原始查询字符串。在重写字符串的末尾。
  • 我必须做两件事:像你提到的那样更改重写规则,还要将 uri_protocol 更改为 ORIG_PATH_INFO。两者都解决了问题
【解决方案2】:

这似乎是 CodeIgniter 设计方式的限制,而不是您的路由和/或.htaccess 的结果。有人提交了错误报告here。但是,您可以像这样使用 _remap() 函数,而不是使用它并将 mymethod 代码添加到默认控制器的 index 方法中:

function _remap($method)
{
    if ($method == 'index' && count($_GET) > 0)
    {
        $this->mymethod();
    }
    else
    {
        $this->index();
    }
}

【讨论】:

  • 从给出的描述来看,作者没有启用查询字符串。如果他这样做了,给出的示例 URL 将是 mysite.com/?c=mycontroller&m=mymethod 而不是 /mycontroller/mymethod。
  • 另外,除非他在做特别特别的事情,否则 CI 会清空 $_GET,所以它总是空的。
  • @ebynum:根据他的帖子,使用控制器和方法访问完整的 URL 包括一切正常:“也 mysite.com/mycontroller/mymethod/?ref=p&t2=455 工作正常”。但你可能是对的。
【解决方案3】:

使用以下配置,它对我来说很好。 http://www.site.com/?x=y 被路由到默认控制器的 index 方法。

.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]

system/application/config/config.php

$config['base_url'] = "http://www.site.com/";
$config['index_page'] = "";
$config['uri_protocol'] = "PATH_INFO";
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?';

记得添加吗?到 URL 中允许的字符列表。也许这会导致问题。我刚刚使用全新安装的 CodeIgniter 尝试了这个设置,它运行良好。这些是我唯一需要做的改变。

【讨论】:

    【解决方案4】:

    这是我解决它的方法: 1.我在.htaccess中有一个标准的重写:

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

    这可能是无关的,但是...

    1. 在 index.php 中我添加的应用程序文件夹外的文件:

    $pattern = "/\?.*$/"; $替换=''; $_SERVER['REQUEST_URI'] =preg_replace($pattern, $replacement, $_SERVER['REQUEST_URI']);

    这会阻止 codeigniter 尝试在请求中使用查询字符串。现在你真的需要 $_GET 变量吗?它们需要从 $_SERVER['REDIRECT_QUERY_STRING'] 中解析出来,如果你像上面那样使用 mod_rewrite 将被设置。

    【讨论】:

      【解决方案5】:

      试试 CodeIgniter Super .htaccess file

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-05
        • 1970-01-01
        • 1970-01-01
        • 2012-07-10
        相关资源
        最近更新 更多