Codeigniter: Mixing segment-based URL with querystrings

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.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Posted on March 9, 2009 at 5:58 pm by Eldee · Permalink
In: PHP Tutorials · Tagged with: , , ,

30 Responses

Subscribe to comments via RSS

  1. Written by david on March 9, 2009 at 8:57 pm
    Permalink

    The local solution won’t work because the globals are sanitized before the controller is loaded.

    A very hackish solution without enabling query strings that does work is to use a pre_system hook to catch the GET global values in a constant.

  2. Written by webmaster on March 10, 2009 at 10:29 am
    Permalink

    hi david
    Thanks for the feedback. but I tested using

    $config['uri_protocol'] = “PATH_INFO”;
    $config['enable_query_strings'] = FALSE;

    and within the controller constructor function

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

    it seems to work for my application. maybe my article wasn’t clear that uri_protocol still needs to be set to PATH_INFO.

  3. Written by david on March 10, 2009 at 6:54 pm
    Permalink

    Are you putting the parse_str($_SERVER[’QUERY_STRING’],$_GET); line in your config? Your article directions are to put it in the constructor.

    I had the uri_protocol set to PATH_INFO when i tested it.

  4. Written by david on March 10, 2009 at 7:03 pm
    Permalink

    I just became aware since my hackish solution uses a constant why not put it in the constants.php file which is a part of the framework since the 1.6.2 version.

    define(‘MY_GET’,$_GET);

  5. [...] – Design, Style, & make it work with PHP & Ajax | Noupe (tags: Form tutorial css html) Codeigniter: Mixing segment-based URL with querystrings (tags: PHP CodeIgniter) Anonymous Functions and Closures (tags: PHP programming) jQuery Sequential [...]

  6. [...] Ask About PHP blog has a tutorial offering CodeIgniter users an alternative to the “no normal GET variables allowed” [...]

  7. Written by webmaster on March 11, 2009 at 6:23 pm
    Permalink

    hi david
    the reason why parse_str($_SERVER[’QUERY_STRING’],$_GET); only goes into the constructor, is so that it’s only enabled for that particular controller.

    this was just to illustrate how to do a segment/querystring mix that works within a particular controller, and not application-wide.

  8. Written by craig on March 13, 2009 at 5:37 pm
    Permalink

    so this not work ?

  9. Written by jeremy on April 3, 2009 at 9:45 pm
    Permalink

    I’ve tried this, and it works to an extent.

    However, with this setup, I need to use the index.php in the URL, even though I have .htaccess setup to rewrite the URLs.

    If I don’t use index.php, I am not directed to the correct controller and just get the welcome screen.

    Has anyone else noticed this?

  10. Written by webmaster on April 4, 2009 at 4:44 pm
    Permalink

    hi jeremy, thanks for the observation. I’ll give it a try and let you know if I get the same outcome.

  11. Written by Nicruo on April 17, 2009 at 5:48 am
    Permalink

    Hi there.
    Just posting to say that this solution worked perfectly in my case. The htaccess problem that jeremy sad isn’t appening to me, so it’s all good here.
    Thanks alot for your tutorial.

  12. Written by Infovore » Bookmarks for April 26th through April 27th on April 28, 2009 at 1:00 am
    Permalink

    [...] Codeigniter: Mixing segment-based URL with querystrings | Ask About PHP "So, is it possible to mix segments and querystring?" Sort of, maybe, seems to be the answer. (tags: codeigniter php routing url hack ) [...]

  13. Written by Colin on July 5, 2009 at 1:13 pm
    Permalink

    What a waste. Use the URI class’ uri_to_assoc method and then you don’t need to worry about the order of key value params.

  14. Written by webmaster on July 7, 2009 at 8:07 pm
    Permalink

    Hi Colin, thanks for pointing out the uri_to_assoc method in the URI class. Will check it out.

  15. Written by Ronggur on September 3, 2009 at 5:21 pm
    Permalink

    hi, i don’t know why, but my code work fine in my localhost using both query string and segment, but when i put it at server.. it gives me 404 page

  16. Written by Jay Garcia on November 19, 2009 at 4:02 am
    Permalink

    Thanks for this post dude. It’s what has gotten me over a pretty big hump when. I’m new to CI and am going to use it to distribute the example code for my book “Ext JS in action”.

  17. Written by Tom on November 26, 2009 at 4:18 am
    Permalink

    I was very exited to use this function but the finding out that it doesn’t work when you use the .htaccess option to remove “index.php” like jeremy said disappoints me.

    Off to find another way to do this.

  18. Written by Niro on December 10, 2009 at 5:40 am
    Permalink

    Yep. doesnt work with htaccess file. too bad.

  19. Written by dave on January 25, 2010 at 8:13 am
    Permalink

    to make your .htaccess work as intended, try removing the question mark from the following rewriterule where it occurs (twice in my htaccess):

    RewriteRule ^(.*)$ /index.php?/$1 [L]

    becomes

    RewriteRule ^(.*)$ /index.php/$1 [L]

    works a treat for me ;)

  20. Written by Chris on March 8, 2010 at 6:30 pm
    Permalink

    cool sounds good, been looking for this info for ages will try tonight :D thanks

  21. Written by Mark on March 20, 2010 at 1:59 am
    Permalink

    Works-for-me. A beautifully simple solution. I’ve not been using the solution for long, and if I encounter any problems I’ll post again. I’ve done only what was suggested in the post above.
    M

  22. Written by igniteflow on March 27, 2010 at 12:07 am
    Permalink

    Seriously, thank you! this post has solved a problem that’s been plaguing me for weeks. Facebook appends query strings to app URLs and they had been crashing my app and causing me hair loss.

  23. Written by someone on April 19, 2010 at 7:03 pm
    Permalink

    This solved my problem!! Thanks for this I was struggling a lot trying to get my stuff to work :)

  24. Written by Michael Ozeryansky on May 17, 2010 at 11:05 am
    Permalink

    Thanks! This is just what I wanted!

  25. Written by Ajith on May 27, 2010 at 6:56 pm
    Permalink

    Thank you for valuable information.

  26. Written by jd on June 29, 2010 at 2:21 pm
    Permalink

    Thanks for the info. Crazy that CodeIgniter doesn’t handle querystrings reliably.

  27. Written by Richard on August 20, 2010 at 2:27 pm
    Permalink

    How to put and htaccess to remove the
    location/projectname/controller/method/id

    I want to convert the link above to
    location/projectname/how-to-build-business/id

    Please help me guys.

  28. Written by Richard on August 20, 2010 at 3:01 pm
    Permalink

    Im try to write and htacces that will change the url
    This codeigniter implementation

    http://yourdomain.com/projectname/controller/method/title
    http://yourdomain.com/projectname/profile/views/how to become a good leader

    and I want to convert that using htaccess into
    http://yourdomain.com/projectname/how to become a good leader/id

    How to remove the controller/method in the codeigniter using htaccess.
    Please help me guys.

  29. Written by Richard on August 20, 2010 at 4:10 pm
    Permalink

    Hill gud guys. If you can help im tring to code and htaccess for the url rewrite
    This is how its look like.
    Original url http://www.site.com/controller/method/title
    Sample
    http://www.site.com/page/views/title of the page

    I would to change that using htaccess into this url below.
    http://www.site.com/title of the page/1
    1 is the id and title of the page is the title.
    Please help me.

    Thanks.
    This is using codeigniter.

  30. Written by Juan on September 2, 2010 at 9:26 pm
    Permalink

    Works like a charm!

Subscribe to comments via RSS

Leave a Reply