How to make CodeIgniter accept “query string” URLs

2019-01-14 09:49发布

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?

9条回答
我命由我不由天
2楼-- · 2019-01-14 10:25

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.

查看更多
女痞
3楼-- · 2019-01-14 10:25

This is actually tested and confirmed

It works with any method you like; giving you freedom to mix match the query string and / segment approach (as opposed to the previous responses)

either you want to use:

example.com/my/group/?id=5

(please note the trailing / before ?). OR

 example.com/my/group/5 

(depending on your url pattern definitions in the router file). OR EVEN

example.com/index.php/?my/group/?id=5

(though that looks awkward enough)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

and in you codigniter's config/config.php file, set

$config['uri_protocol'] = 'AUTO';
$config['enable_query_strings'] = TRUE;
查看更多
地球回转人心会变
4楼-- · 2019-01-14 10:29

CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you'll see these items:

enter code here $config['enable_query_strings'] = FALSE;

$config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm';

If you change "enable_query_strings" to TRUE this feature will become active.

查看更多
三岁会撩人
5楼-- · 2019-01-14 10:30

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]
查看更多
聊天终结者
6楼-- · 2019-01-14 10:31

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.

查看更多
在下西门庆
7楼-- · 2019-01-14 10:36

You may change URI PROTOCOL in your config file to

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

and

  $config['enable_query_strings'] = FALSE;

It'll accept query strings and allow your URLs. Worked for me :)

查看更多
登录 后发表回答