Welcome Guest, Not a member yet? Register   Sign In
regular expression for URI routing

[eluser]Tomas Sundvall[/eluser]

I'm trying to create an route that makes all urlConfused call a certain function in a certain controller.

So if you for example go to
http://mypage/page1, http://mypage/start/index/page1 will "be used".

I've got it to work easily with:
//$route[':any'] = "start/index";

The problem is that there is one exception, and that is when you go to
http://mypage/admin, then I want the admin controller to "be used".

I tried to create a regular expression that made everything go to "start/index" except if you wrote admin, but I wasn't successfull.

Does anyone know how to do that?


Just make a route for admin and put it before the other one.
CI will use the first route that matches your url.

Tom, route can work only for same controller routing. route can't route from $route[':any'] = "start/index" (controller to other controller).
or $route['controller1/:any'] = "other_controller/index";

Avatar, where did you get that idea? That is totally incorrect.

I made a mistake. sorry

@Tom, dont use $route[':any'] = "go to somewhere"; because it has highest routing priority.

[eluser]Tomas Sundvall[/eluser]
Thanks for your answers!

I tried to write:
$route[’admin’] = 'admin/login'
$route[’:any’] = 'start/index'

So that dosen't work since $route[’:any’] has the highest routing priority? So then the problem must be solved using a regular expression that routs anything except 'admin' (case insensitive) to 'start/index'?

I tried with:
$route['^[^admin].*'] = "start/index";

It worked a little bit better. But the problem was that evrything that starts with admin is not sent to "start/index". So if I for example write http://mypage/administration, then I'm not being routed to "start/index".

I don't really know how to rewrite it so it only not sends admin to "start/index".

Does anyone have an idea of how to do this? :-).


No. The routes are tried IN ORDER.
So if the 'admin' route is before the 'any' route, it should work just fine!
If the two routes as you have written them here don't work, there is something else wrong.

You need to be explicit in your routes! They will do exactly what you tell them to do. If you want anything with admin as the controller segment redirected to the admin controller, where you have an "override all routes" route below, you must explicitly define it (and any others that might occur later on)...

$route['admin/(:any)'] = 'admin/$1';
$route[':any'] = 'start/index';

Theme © iAndrew 2016 - Forum software by © MyBB