How to keep query strings from breaking your site’s default page in CodeIgniter Aug 28
CodeIgniter’s out-of-the-box handling of query strings leaves a bit to be desired. There are a few quirks noted in forums and bug reports that suggest that CodeIgniter was never meant to power sites that pass or receive data via the common method of query strings (i.e., those little ?name=value appendages in URL’s).
However, one of the biggest issues with query strings + CodeIgniter is that, by default on many setups of CodeIgniter, adding a query string to the URL of your default page (e.g., example.com or yoursite.com) will throw a 404 error. Essentially, CodeIgniter thinks that the query string is specifying a controller file which obviously doesn’t exist. I found this out – like others – when Google was adding “?gclid=random_id_number” to my AdWords referral URL’s and thus every clickthrough was seeing a 404 Error instead of my site’s homepage.
I have fixed this problem. And, in doing so, I’ve also eliminated many of the quirks of query strings in CodeIgniter. Want to implement these fixes in your setup? It’s easy. Instructions and files are below.
Note #1: This how-to will work with both CodeIgniter 1.x and CodeIgniter 2.x but keep in mind folder locations as they differ for these versions.
Note #2: These likely only work on Apache servers but I haven’t tested elsewhere.
Step 1: Modify the URI Library
We need to stop CodeIgniter from thinking the query string is a controller while also retaining the query string for use later when retrieving $_GET data with $this->input->get(). This URI library extension will do both of these things. First, it strips the query string out of the URL so that CodeIgniter doesn’t get confused. Second, it stores the query string in a defined PHP variable for our use later, when retrieving $_GET data.
Download the extended URI library.
For CodeIgniter 1.x, upload as application/libraries/MY_URI.php.
For CodeIgniter 2.x, upload as app/core/MY_URI.php.
Step 2: Modify the Input Library
In step 1, we eliminated the query strings from the stored URL so that CodeIgniter wouldn’t confuse them as controller names. However, in doing so, we also broke the ability to extract $_GET data with $this->input->get(). So, this modified input library will use the constant we defined in step 1 to retrieve $_GET data via the Input library.
Download the extended Input library.
For CodeIgniter 1.x, upload as application/libraries/MY_Input.php.
For CodeIgniter 2.x, upload as app/core/MY_Input.php.
Success!
Your pages won’t break and you can now access $_GET data easily via CodeIgniter’s standard methods. Enjoy!





