You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CodeIgniter路由中使用阿拉伯字母?阿拉伯参数路由失效求助

Fixing CodeIgniter Routing Failure with Arabic Characters

Hey there, I’ve run into this exact issue before with non-ASCII characters in CodeIgniter routes, so let’s break down why this is happening and how to fix it step by step.

Why Arabic Characters Break the Route

The default (:any) placeholder in CodeIgniter’s routing system uses the regular expression [^/]+ under the hood. This pattern only matches ASCII characters (a-z, 0-9, and basic symbols), but Arabic characters fall into the Unicode range—which isn’t included in that match. That’s why your route works perfectly with English but fails when using Arabic letters.

Solutions to Fix the Routing

Instead of relying on (:any), define a custom regex pattern that explicitly matches Unicode letters (including Arabic), numbers, and any other characters your product slugs might use. Here’s how to adjust your route:

// Replace (:any) with a regex that supports Unicode letters/numbers
$route['rental/([\p{L}\p{N}_-]+)'] = 'site/landing/display_product_detail/$1';

Let’s break down the regex components:

  • \p{L}: Matches any Unicode letter (covers Arabic, English, and all other non-ASCII alphabets)
  • \p{N}: Matches any Unicode number
  • _-: Includes underscores and hyphens (adjust these if your product slugs use other symbols like periods or ampersands)
  • The + ensures it matches one or more of these valid characters.

If your slugs include spaces or additional symbols, you can expand the character set—just avoid adding / (since that’s the route separator). For example:

$route['rental/([\p{L}\p{N}_\-&@\s]+)'] = 'site/landing/display_product_detail/$1';

2. Update CodeIgniter’s Permitted URI Characters

CodeIgniter restricts allowed URI characters by default for security. You need to explicitly permit Arabic characters in your config. Open application/config/config.php and update the permitted_uri_chars setting:

// Allow Unicode letters, numbers, and common symbols
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-\p{L}';

If you want more control, you can specify the exact range of Arabic characters (Unicode U+0600 to U+06FF):

// Explicitly include Arabic letters
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-أ-ي';

3. Verify Server Configuration (If Issues Persist)

Sometimes server settings block Unicode characters in URLs:

  • Apache: Add AllowEncodedSlashes On to your .htaccess or Apache config to support encoded Unicode characters.
  • Nginx: Include charset utf-8; in your server block, and ensure your location block correctly passes URI parameters to CodeIgniter.

Bonus: Test and Validate

After making these changes, test your route with an Arabic slug (e.g., example.com/rental/سيارة-مرسيدس). You can debug by adding var_dump($1); in your display_product_detail method to confirm the parameter is being passed correctly. Also, double-check that your controller, views, and database all use UTF-8 encoding to avoid character display issues.

内容的提问来源于stack exchange,提问作者Marwan Elkhoudary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 22:57:44