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.
In: PHP Tutorials · Tagged with: codeigniter, querystring, segments, url

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.
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.
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.
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);
Permalink
[...] – 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 [...]
Permalink
[...] Ask About PHP blog has a tutorial offering CodeIgniter users an alternative to the “no normal GET variables allowed” [...]
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.
Permalink
so this not work ?
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?
Permalink
hi jeremy, thanks for the observation. I’ll give it a try and let you know if I get the same outcome.
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.
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 ) [...]
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.
Permalink
Hi Colin, thanks for pointing out the uri_to_assoc method in the URI class. Will check it out.
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
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”.
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.
Permalink
Yep. doesnt work with htaccess file. too bad.
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
Permalink
cool sounds good, been looking for this info for ages will try tonight
thanks
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
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.
Permalink
This solved my problem!! Thanks for this I was struggling a lot trying to get my stuff to work
Permalink
Thanks! This is just what I wanted!
Permalink
Thank you for valuable information.
Permalink
Thanks for the info. Crazy that CodeIgniter doesn’t handle querystrings reliably.
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.
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.
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.
Permalink
Works like a charm!
Permalink
[...] http://qeremy.com/codeigniter-pager-class (Sayfalama) http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html [...]
Permalink
[...] So the first and foremost tweak that we need to do is configure Codeigniter to mix the segments and querystring. this can be done with this Howto article. [...]
Permalink
$_SERVER["REQUEST_URI"]
Permalink
Dave’s comment fixed my .htaccess problem that the OP doesn’t cover. Thanks Eldee and commenters!
http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html#comment-18841
Permalink
Thanks, this worked great, no problems!
Permalink
niced
Permalink
Thanks for this, really helped me
Permalink
I tried this method and was getting 302 and 404 errors.
I removed the question mark following “index.php” in my .htaccess rewriterule as dave suggested above. Works great now. Thanks for the article and comments everyone.
Permalink
[...] Series andrewroland zend lucene librarycodeigniter 2.0 workshoplearncodeigniter basics Sponsors Top Rated TutorialsUsing Elliot Haughin's Twitter API with Codeigniter Part 2 (2 votes)CodeIgniter Active Record class and INSERT IGNORE (2 votes)Codeigniter Project Development from scratch to app Part 6 (2 votes)Tags2.0 active record ajax amplio.ch andrewroland.com api askaboutphp beginner class CMS codesamplez controller country db dragffy.com drop down egypt error flashdata form from scratch helper html index.php install jquery login lucene MVC net.tutsplus profiler reactor registration search session simplycodeigniter twitter upload URL validation variables view views workshop zend Sponsors LinksCodeigniter: Mixing segment-based URL with querystrings [...]
Permalink
Thx a lot dude, your solution fixed my problem with a facebook app.
Permalink
Enjoy this….
RewriteEngine On
RewriteRule ^gallery/([a-z0-9_-]+)\.html$ index.php/page/gallery/$1 [L]
RewriteRule ^gallery/([a-z0-9_-]+)/([a-z0-9_-]+)\.html$ index.php/page/gallery_detail/$1/$2 [L]
RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/index/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|asset|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
1> Example for gallery module [controller will be gallery and variable will move in function]
2> By default on http://www.example.com/test.html [page controller > index function > test will become variable]
You can make any permalink with any controler with any function. Everything is possible now.
Permalink
[...] to this post http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html, I have managed to find the necessary settings to allow [...]
Permalink
Can you make a version of this for CI 2.02?
Permalink
Wow. That really saved us. Thanks!
Permalink
Im really confused by this post. Im using CI 2.1.0 and all I need to do to access any url parameter is just use
$_GET
so if I had
mydomain.com/member/id/12345/?code=B200
and I wanted access to code I just use
$my_code = $_GET['code'];
and then clean up $my_code
Im not sure why everyone is going about this with hacks when this simply works.
Permalink
(http://localhost/ab1/index.php/search?d=Guest)work but i want this
http://localhost/ab1/index.php/search/d/Guest)
how is it possible ? email me
Permalink
I enjoy this post, enjoyed this one regards for publishing.
Permalink
It worked for me and saved me a lot of time. Thank you very much for sharing your knowledge