codeIgniter默认的配置下是不允许URL中包含非ASCII字符的,正常情况下,如果我们输入网址http://www.wfuyu.com/se/数据库/
那么CI会毫不客气的告诉你:
The URI you submitted has disallowed characters.
如何让ICI支持中文url呢
我们只需要在application/core/下增加一个文件MY_URI.php,其内容为:
<?php class MY_URI extends CI_URI { function _filter_uri($str) { if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE) { // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards $str = urlencode($str); // 增加的代码 if ( ! preg_match("|^[".str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", $str)) { show_error('The URI you submitted has disallowed characters.', 400); } $str = urldecode($str); // 增加的代码 } // Convert programatic characters to entities $bad = array('$', '(', ')', '%28', '%29'); $good = array('$', '(', ')', '(', ')'); return str_replace($bad, $good, $str); } }
这样CI就支持中文url了,但是,如果中文有空格,还是会报错
还要改个配置项 在config里面,否则的话无法传输空格
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_/-';
改成
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_/+/-';