3

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

0

4 Answers 4

4

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);
}
4
  • would you please have a look at this: stackoverflow.com/questions/56229130/…
    – PHP User
    May 21, 2019 at 8:49
  • No need to change or config each line with your solution, you can simply add this line to the .htaccess file as follow: RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]
    – Jodyshop
    Jul 4, 2020 at 20:14
  • Hi, may i know how to write routes if the arabic values is dynamic? $route['^(en|ar)/getdetails/(:any)'] = "controller/getdetails/$1"; localhost/project/ar/getdetails/arabic_value is the url calling from browser!
    – mevr
    Feb 4, 2021 at 11:29
  • 1
    @youv if you created the MY_url_helper as described in the question, then it should work
    – Waqleh
    Feb 5, 2021 at 12:26
2

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

RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]
2
  • Wow! worked like a charm, thanks for the life-saving trick :)
    – Jodyshop
    Jul 4, 2020 at 20:13
  • I have tried this trick but haven't worked for me, may i know the possibilities of failing? Thanks!
    – mevr
    Feb 4, 2021 at 11:31
0

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.

1
  • my issue is with the routes not with the permitted_uri_chars, i tested it by leaving the $config['permitted_uri_chars'] = ''; empty
    – Waqleh
    Jan 9, 2014 at 14:04
-1

use this->

urldecode(auto_link($url))

Not the answer you're looking for? Browse other questions tagged or ask your own question.