phpcodeigniterutf-8arabic-supportfarsi

How to use Codeigniter routes with UTF-8 like Arabic characters 404


I am trying to use Arabic/Persian/Farsi character in the URL as a slug in Codeigniter like so:

I created MY_url_helper.php helper file to add the Arabic characters as accepted characters in the url_title Codeigniter function

/**
 * Create URL Title that allows Arabic characters
 *
 * Takes a "title" string as input and creates a
 * human-friendly URL string with a "separator" string 
 * as the word separator.
 *
 * @access  public
 * @param   string  the string
 * @param   string  the separator
 * @return  string
 */
function url_title($str, $separator = '-', $lowercase = FALSE) {
    if ($separator == 'dash') {
        $separator = '-';
    } else if ($separator == 'underscore') {
        $separator = '_';
    }
    $q_separator = preg_quote($separator);
    $trans = array(
        '&.+?;' => '',
        '[^[U+0621]-[U+064a]a-z0-9 _-]' => '',
        '\s+' => $separator,
        '(' . $q_separator . ')+' => $separator
    );
    $str = strip_tags($str);
    foreach ($trans as $key => $val) {
        $str = preg_replace("#" . $key . "#i", $val, $str);
    }
    if ($lowercase === TRUE) {
        $str = strtolower($str);
    }
    return trim($str, $separator);
}

Then I had to enable Arabic characters as permitted uri character in the config.php file:

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';  
$config['permitted_uri_chars']  .= 'لإ-لآ-أ-لآ-ض-ص-ث-ق-ف-غ-ع-ه-خ-ح-ج-د-ط-ك-م-ن-ت-ا-ل-ب-ي-س-ش-ئ-ء-ؤ-ر-ل-ا-ى-ة-و-ز-ظ-إ';

I tested it like so

http://dev/ar/pages/index/مهمتنا

and everything work, what is missing is the routes.

Just as this works in english:

$route['^(en|ar)/mission'] = "pages/index/mission";

I would like for this to work in arabic:

$route['^(en|ar)/مهمتنا'] = "pages/index/مهمتنا";

This gives 404

I tried emptying the permitted_uri_chars like so:

$config['permitted_uri_chars'] = '';

I still got the same exact issue; the link without routes work fine and the one with the route gave me 404


Solution

  • The solution is as simple as this:

    in the routes.php config file do the following:

    $mission = urlencode('مهمتنا');
    $route['^(en|ar)/'.$mission] = "pages/index/".$mission;
    

    no need to change the permitted_uri_chars in the config.php config file.

    if the error was with the permitted_uri_chars the output would have been 400 "The URI you submitted has disallowed characters." not 404 as you can see here at system/core/URI.php, on line 231:

    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);
    }