Welcome Guest, Not a member yet? Register   Sign In
Using routes to ignore query strings
#1

[eluser]efishant[/eluser]
So I'm facing an interesting problem. Our website is tailored for mobile users. Some carriers force append query strings to the end of their user's page requests. Because of this, we need a solution that allows us to effectively ignore the query strings. Below are a couple of constraints that I'm forced to work with. (please don't start a discussion as to why we have these constraints Smile

1. We can't use .htaccess or edit the .conf file.
2. We definitely can't enable query strings in the config.php file


Here's what I've tried so far:
Code:
$route['(.*)\?(.*)'] = "$1";//tried with and without the escape char

However, the outcome is the same with or without this route. Here are a couple of scenarios I'm seeing. (note: I have a default method for my controllers, so my test URLs only contain the controller name)

1. controller?
- the controller loads without any problems
2. controller?this, controller?this=, controller?this=that, controller?this=that&
- 404 page is returned
3. controller?this=that&x;, controller?this=that&anystring=combination
- The controller loads without any problems.

Any ideas as to how I could ignore the query string so that ?(.*) is ignored completely by CI? I can use anything within CI's domain; hooks, routes etc.

Thanks!
#2

[eluser]Seppo[/eluser]
Mmm... what do you have as $config['index_page'] ? Are you using mod_rewrite?

I assume you are not using mod_rewrite, 'cause you said you can't use it, so I think you probably have "index.php" as index_page
Try using
Code:
$config['index_page'] = "index.php?"
instead
#3

[eluser]efishant[/eluser]
We're doing a rewrite to remove index.php from the URLs, but other than that one usage, we can't use .conf or .htaccess for anything else.
#4

[eluser]Seppo[/eluser]
OK...

The problem seems to be with the URI library... do you have $config['uri_protocol'] = 'auto'; ?
If that so, you can try changing the protocol or extending the Uri class this way

Code:
<?php
class MY_URI extends CI_URI {
    function MY_URI()
    {
        parent::CI_URI();
    }

    function _fetch_uri_string()
    {
        if (is_array($_GET) AND count($_GET) == 1)
        {
            $_GET = array();
        }
        return parent::_fetch_uri_string();
    }
}
?>
#5

[eluser]efishant[/eluser]
Thanks for the suggestion :-) . I tried it, but no luck. I'm guessing that it's something CI doesn't have control over Undecided
#6

[eluser]Seppo[/eluser]
I think I know why... in the URI class (lines 68-72), if the uri protocol is auto, the first thing it checks is for one (and only one) get parameter, and if it exists, uses the key as the uri string
Code:
if (is_array($_GET) AND count($_GET) == 1)
            {
                $this->uri_string = key($_GET);            
                return;
            }

That would explain exactly what you are telling... you can try comenting those lines, and see what happens...
#7

[eluser]JamesD[/eluser]
For future ref... IF '.htaccess' was used how would one remedy this problem?

Thanks!

-james
#8

[eluser]JamesD[/eluser]
Nevermind... I've figured it out (well... it's working so far without any problems).

I just added the following 3 lines directly under the 'RewriteEngine On' portion of my '.htaccess':

Code:
RewriteCond %{HTTP_HOST} (www.)?mydomian.com
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://mydomian.com/$1? [R=301,L]

I've only messed with some of the basics of the '.htaccess' file in the past (since it's never really been needed)... maybe it was about time I actually did some actual work with it. :cheese:

-james

P.S.
Of course make sure you replace the 'mydomian.com' with your own domain name. Tongue




Theme © iAndrew 2016 - Forum software by © MyBB