Welcome Guest, Not a member yet? Register   Sign In
URI routing with regular expressions - can't get it done
#1

[eluser]George I[/eluser]
Hi folks,

I may have a false problem here, but I can't seem to make it work.

I'm working on a multilingual website which will be launched with just two languages (Romanian and English) and afterwards it will be completed with other languages as well. I'm working with the i18n library for that.

I have successfully eliminated the "index.php" from the url with .htaccess.

I have a controller named "despre". The URL for this controller would be http://www.mysite.com/lang/despre, where lang is either "ro" or "en", depending on the selected language.

I also want to use nice url's, so I've set some rules in routes.php, like this:
Code:
$route['ro/despre-noi'] = "despre";
$route['ro/about-us'] = "despre";
$route['en/despre-noi'] = "despre";
$route['en/about-us'] = "despre";
This works like a charm, but I want to write it with regexp:
Code:
$route['ro/despre-noi|ro/about-us|en/despre-noi|en/about-us'] = "despre";
When I put it like this, I get a 404 Page Not Found error.

Any ideas?
#2

[eluser]toopay[/eluser]
open your config.php, on config folder

add/change this line
Code:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
to
Code:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-|';

you can add as many regex as you want there...but as a security measure, you should restricted your URLs properly.
#3

[eluser]George I[/eluser]
toopay,

Did that, didn't work. I'm getting quite pissed off, since I have half a dozen controllers, each with about 5 functions and each function with its id's, and everything must be in 2 languages at first, and probably 6 or so later... I NEED to find a quicker way than to write a hundred routing lines...
#4

[eluser]toopay[/eluser]
what you mean didnt work? what version you use? it should work!
#5

[eluser]George I[/eluser]
I'm using CodeIgniter 1.7.3 and I get the same 404 error.
#6

[eluser]Twisted1919[/eluser]
Code:
$route['(ro\/despre-noi|ro\/about-us|en\/despre-noi|en\/about-us)'] = "despre";
You need to escape the / as you would do in a normal regular expression.
#7

[eluser]toopay[/eluser]
If you insist to use some routes like yours, which something like
Code:
$route['ro/despre-noi|ro/about-us|en/despre-noi|en/about-us'] = "despre";
you CAN do it by adding "|" to your $config['permitted_uri_chars']. Thats WORK.

If you are no problems with adding some word/line, you can use Twisted1919 suggestion, which fairly more nice.
#8

[eluser]George I[/eluser]
[quote author="Twisted1919" date="1296681241"]
Code:
$route['(ro\/despre-noi|ro\/about-us|en\/despre-noi|en\/about-us)'] = "despre";
You need to escape the / as you would do in a normal regular expression.[/quote]
Doesn't work this way either, I get the same evil 404.

I'm feeling very stupid right now, but I can't understand why
Code:
$route['ro/despre-noi'] = "despre";
works (without escaping the / character), and
Code:
$route['ro/despre-noi|en/despre-noi'] = "despre";
doesn't, even with the / escaped.
#9

[eluser]George I[/eluser]
[quote author="toopay" date="1296682289"]...you CAN do it by adding "|" to your $config['permitted_uri_chars']. Thats WORK.[/quote]

I swear I did, and I got the same error. In fact, I set $config['permitted_uri_chars'] to '', just to see what happens. Guess what? 404.
#10

[eluser]Twisted1919[/eluser]
Let's try it this way:
Code:
$route['ro/(despre-noi|about-us)'] = "despre";
$route['en/(despre-noi|about-us)'] = "despre";
if the above works, let's do it like:
Code:
$route['(ro/(despre-noi|about-us))|(en/(despre-noi|about-us))'] = "despre";
So ?




Theme © iAndrew 2016 - Forum software by © MyBB