如何在CodeIgniter路由中使用阿拉伯字母?阿拉伯参数路由失效求助
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
1. Use a Custom Unicode-Compatible Regular Expression (Recommended)
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 Onto your.htaccessor 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

