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

4.2k views Asked by At

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

4

There are 4 answers

4
Waqleh On BEST ANSWER

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);
}
1
Ma'moon Al-Akash On

Try to encode the arabic characters, so that instead of using:

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

You might want to use:

$config['permitted_uri_chars'] .= '%D9%84%D8%A5-%D9%84%D8%A2-%D8%A3-%D9%84%D8%A2-%D8%B6-%D8%B5-%D8%AB-%D9%82-%D9%81-%D8%BA-%D8%B9-%D9%87-%D8%AE-%D8%AD-%D8%AC-%D8%AF-%D8%B7-%D9%83-%D9%85-%D9%86-%D8%AA-%D8%A7-%D9%84-%D8%A8-%D9%8A-%D8%B3-%D8%B4-%D8%A6-%D8%A1-%D8%A4-%D8%B1-%D9%84-%D8%A7-%D9%89-%D8%A9-%D9%88-%D8%B2-%D8%B8-%D8%A5';

Also you might want to remove the "-" character from the config item so the "permitted_uri_chars" config item would like:

$config['permitted_uri_chars'] .= '%D8%A2%D8%A7%D8%A8%D9%BE%D8%AA%D8%AB%D8%AC%DA%86%D8%AD%D8%AE%D8%AF%D8%B0%D8%B1%D8%B2%D8%B3%D8%B4%D8%B5%D8%B6%D8%B7%D8%B8%D8%B9%D8%BA%D9%81%D9%82%DA%A9%DA%AF%D9%84%D9%85%D9%86%D9%88%D9%87%DB%8C%D9%8A%DB%B1%DB%B2%DB%B3%DB%B4%DB%B5%DB%B6%DB%B7%DB%B8%DB%B9%DB%B0';

This should solve your issue as those characters are being handled in the encoded form instead of their original values.

Hope this helps.

2
Abd Abughazaleh On

I tried this and it's working with me just add this in .htaccess

RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]
0
Yahya Ayyoub On

use this->

urldecode(auto_link($url))