views:

642

answers:

3

According to CI's docs, CodeIgniter uses a segment-based approach, for example:

example.com/my/group

If I want to find a specific group (id=5), I can visit

example.com/my/group/5

And in the controller, define

function group($id='') {
    ...
    }

Now I want to use the traditional approach, which CI calls "query string" URL. Example:

example.com/my/group?id=5

If I go to this URL directly, I get a 404 page not found. So how can I enable this?

A: 

Modify application/config.php at the line:

$config['enable_query_strings'] = FALSE;

Make this true instead. There are other details you'll have to pay attention to also. See here.

wallyk
That configuration appears to instruct the URL interpreter to use the query string instead of path segments for its controller/method arguments. I'm not really sure that's what the questioner wants.
eyelidlessness
I've reread the question and the question appears to be "how to enable query string URLs?
wallyk
I agree with eyelidlessness. This setting only allows you to use controller and method parameters in the query string URL.
zihaoyu
A: 

After setting $config['enable_query_strings'] = TRUE; in your config.php file, you can use the segment-based approach in conjunction with query strings, but only if you use 2 or more variables (separated by a "&") in the query string like this:

example.com/my/group?id=5&var=something

See this answer for more information.

Colin
Are you saying that if I only have one param (id), then I cannot use query string URL?
zihaoyu
@Peter: If you want the controller and methods as URI parameters (rather than variables in the query string), then yes - based on some brief testing it appears you need to append 2 or more variables to the query string (I don't know why). If only one variable is present, it throws a 404 error.
Colin
+2  A: 

For reliable use of query strings I've found you need to do 3 things

  1. In application/config/config.php set $config['enable_query_strings'] = true;
  2. Again in application/config/config.php set $config['uri_protocol'] = "PATH_INFO";
  3. Change your .htaccess to remove the ? (if present) in the rewrite rule

I use the following

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
WeeJames
I still get a 404 error. CodeIgniter is located at `my.server.com/project/ci`, and there is `my.server.com/project/ci/index.php`, `my.server.com/project/ci/.htaccess`, and `my.server.com/project/ci/system/application`.
zihaoyu
WeeJames
@WeeJames Thanks, I put `$id=$this->input->get('id')` at the beginning of my controller, and set `$config['uri_protocol'] = "QUERY_STRING";` in config.php. But `example.com/my/group?id=5` still does not work. I got the 404 error. What else should be done?
zihaoyu
YOu need to modify the `application/config/config.php` line `$config['enable_query_strings'] = FALSE;` to be `true`. Leave the uri_protocol at `auto`.
WeeJames