Codeigniter, by default crafts it’s URL in a search engine friendly format. It uses a segment-based approach and does away with the all familiar querystring format that developers have been using for many years to pass parameters via the URL into their applications.

Codeigniter does allow you to turn on the querystring capability, but that would mean you have to use a pure querystring approach, foregoing the segment-based approached.

So, is it possible to mix segments and querystring?

Thankfully, Codeigniter can be configured to mix the segments and querystring, and all it takes are some tweaks in your application/config.php file and perhaps an additional line of code.

Say you would like to have a URL like so:

example.com/product/search/?pname=test&pid=2

By default, Codeigniter would prefer you to craft the equivalent segmented URL, and it would probably look like this.

example.com/product/search/test/2

One of the limitations of this is that the order of the segments becomes important. In your code, you have to remember that the segment after /search/ is the ‘pname’ variable and the segment after that is the ‘pid’ variable. You won’t have this issue if you used a query string.

In most cases, this is not really a show stopper. But there are times when you have no choice but to include querystrings into your URL. For example, a legacy application that interfaces with your application perhaps?

If you follow the (excellent) Codeigniter user guide, and you turn on the querystring capability , Codeigniter will now function with a URL like this:

example.com/?c=product&m=search&pname=test&pid=2

Which is also not the most ideal.

This is what you can do if you need to have a mixed segment and querystring approach. You can enable this either globally for your whole application or just locally within a controller.

Option 1: Mixing Globally
To enable this globally, go to your application/config.php file, and look for $config['uri_protocol'] variable and change it to:

$config['uri_protocol'] = "PATH_INFO";

After that, find the $config['enable_query_strings'] variable and change it to:

$config['enable_query_strings'] = TRUE;

Your application should now accept both segments and querystrings.

Option 2: Mixing Locally
If you don’t want to enable this capability across your whole application, you can restrict it to only the controllers you want.

You can do this by going to the application/config.php file and switching $config['enable_query_strings'] back to ‘FALSE’.

$config['enable_query_strings'] = FALSE;

Leave the $config['uri_protocol'] as PATH_INFO

$config['uri_protocol'] = "PATH_INFO";

and then you can add this line to your controller’s contructor method:

parse_str($_SERVER['QUERY_STRING'],$_GET);

So if you do a test now, only the controller with the above line of code will be able to accept mixed segment and querystring URLs.