你可以像这样准备你的配置:
$config['base_url'] = 'your_base_url';
$config['index_page'] = ''; // empty string
$config['enable_query_strings'] = FALSE; // keep it to default FALSE, you can use it even without enabling it here
和你的.htaccess 像这样:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
我想就是这样,现在您可以像这样同时使用segments 和query_strings:
http://localhost/codeigniter/?c=welcome&m=index
http://localhost/codeigniter/welcome/index
http://localhost/codeigniter/index.php/welcome/index
http://localhost/codeigniter/index.php?c=welcome&m=index
http://localhost/codeigniter/index.php/?c=welcome&m=index
我已经在我的环境中进行了测试,并且效果很好。
编辑:在我的 codeigniter 模板中,我扩展了我的核心路由器并稍微更改了 _set_routing() 方法,如下所示:
当然改核心系统是不好的做法,所以需要扩展核心路由器文件,在application/core下创建MY_Router.php,并使其扩展CI_Router如下:
class MY_Router extends CI_Router
{
protected function _set_routing()
{
if (file_exists(APPPATH.'config/routes.php'))
{
include(APPPATH.'config/routes.php');
}
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
{
include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
}
if (isset($route) && is_array($route))
{
isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
unset($route['default_controller'], $route['translate_uri_dashes']);
$this->routes = $route;
}
$_c = trim($this->config->item('controller_trigger'));
if ( ! empty($_GET[$_c]))
{
$this->uri->filter_uri($_GET[$_c]);
$this->set_class($_GET[$_c]);
$_f = trim($this->config->item('function_trigger'));
if ( ! empty($_GET[$_f]))
{
$this->uri->filter_uri($_GET[$_f]);
$this->set_method($_GET[$_f]);
}
$this->uri->rsegments = array(
1 => $this->class,
2 => $this->method
);
}
else
{
if ($this->uri->uri_string !== '')
{
$this->_parse_routes();
}
else
{
$this->_set_default_controller();
}
}
}
}
现在您拥有两全其美的优势,您将保持您的细分市场不受影响,但始终会寻找获取c=&m=,我希望它足够清楚。
我们将所有内容保持为默认值,并使其使用 c 和 m 检查获取请求,这与 enable_query_strings 所做的非常相似,但没有启用它并弄乱了任何东西以继续处理分段。.