codeIgniter默認的配置下是不允許URL中包含非ASCII字符的,正常情況下,如果我們輸入網址http://www.vxbq.cn/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~%.:_/+/-';