Welcome Guest, Not a member yet? Register   Sign In
Adding querystrings to the url results in a 404. I have tried EVERYTHING.
#1

[eluser]PrestonCI[/eluser]
Hi everyone,

So, when I add a querystring to any of my urls, I get a 404 page.

I need to use segments, so $config['enable_query_strings'] is not an option.

I don't even need to access the querystring values, just allow them. I could strip them off for all I care, I just need to get rid of the 404.

I'm aware of this epic thread, and have tried almost all of the workarounds there with no success.

Please help if you can, thanks!

.htaccess

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

config.php

Code:
$config['uri_protocol']    = "AUTO";

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?=&';

$config['enable_query_strings'] = FALSE;

routes.php

Code:
// Custom Routes
$route['games/(:any)/(:num)'] = "games/platforms/$1/$2";
$route['games/(:any)/(:any)'] = "games/details/$1/$2";
$route['games/(:any)'] = "games/platforms/$1";
$route['games'] = "games/index";
#2

[eluser]PrestonCI[/eluser]
Oh my. I've solved it. The answer was right here all along.

The solution that worked for me can be found at: http://github.com/dhorrigan/codeigniter-query-string

THANK YOU, Dan Horrigan!

IMO, CodeIgniter needs to address this issue in the core ASAP as many third party sites (Facebook, etc) add querystrings and you don't want users following those links to get a 404. That's just crazy.
#3

[eluser]WanWizard[/eluser]
Forget trying to trick CI into getting this working. To have this working in all circumstances, quite a few changes to the code are needed.

Set the uri_protocol to PATH_INFO (in AUTO, routing fails if the URI has exactly 1 GET parameter), and then add this method to your code:
Code:
public function parse_querystring()
{
    if ( (int) CI_VERSION >= 2)
    {
        $this->security =& load_class('Security');
    }

    $_GET = array();
    if (!empty($_SERVER['REDIRECT_QUERY_STRING']))
    {
        $pairs = explode("&", $_SERVER['REDIRECT_QUERY_STRING']);
    }
    elseif
    {
        $pairs = explode("&", $_SERVER['QUERY_STRING']);
    }
    else
    {
        return;
    }
    foreach ($pairs as $pair)
    {
        list($k, $v) = array_map("urldecode", explode("=", $pair));
        if ( (int) CI_VERSION < 2)
        {
            $_GET[$k] = $this->input->xss_clean($v);
        }
        else
        {
            $_GET[$k] = $this->security->xss_clean($v);
        }
    }
}

When called, it will repopulate the $_GET array. It is compatible with both CI 1.7.2 and CI 2.0.
#4

[eluser]PrestonCI[/eluser]
Thanks WanWizard.

But what's wrong with the hook method? I've tested it with all my urls and it seems to work in every circumstance.

I already tried a few of the PATH_INFO fixes in the other thread to no avail, and the hook method seems to be a clean fix.
#5

[eluser]WanWizard[/eluser]
For starters, it doesn't work on setups that have REDIRECT_QUERY_STRING instead of QUERY_STRING...

The main issue with AUTO is that it first checks for one (and exactly one) $_GET parameter. If found, it uses that for URI routing instead of the other solutions. so URI's with '?foo=bar' fail with a 404, but '?foo=bar&bar=foo' works with AUTO. Hence the change to PATH_INFO. But it is far from fool-proof, some setups are known to fail with PATH_INFO.

The hook is imho a kludge to kill the QUERY_STRING so this doesn't happen...

It is very difficult to come up with a hack that fits all. Ellislabs should rewrite the URI/Input logic to properly support query strings, it's the only proper solution.
#6

[eluser]pbreit[/eluser]
I can't use CodeIgniter until this is fixed. EllisLabs, please!!




Theme © iAndrew 2016 - Forum software by © MyBB