Route Everything, Except These Controllers

I was playing with CodeIgniter routing at work. I wanted to have a catch-all route but I needed to exclude a certain controller, or controllers, from that catch-all route. As an example, I wanted to have http://www.example.com/(ANYTHING) route to the “page” controller. http://www.example.com/policies/shipping would route to the “policies” controller and then to the “ship” method. From there the “page” controller would split up all of the segments.

I started out with this:

$route['policies/shipping'] = "policies/ship";
$route['(:any)'] = "page";

The “policies/ship” route would be caught by the “policies” controller and everything else would be caught by the “page” controller. Except the “page” controller was getting everything.

I needed to exclude certain controllers from the catch-all route. After a Google search, I came up with nothing. So I started working on it myself. Here is what I cam up with:

$route['policies/shipping'] = "policies/ship";
$route['^(?!policies|controllerA|controllerB)\S*'] = "page";

So far it seems to work perfectly. Hopefully this helps someone else out.

2 Responses to Route Everything, Except These Controllers

  1. Hi David! Thanks for this, I just made use of it and it worked very nicely for me. Might eventually make the “controllers” part of it automated based on a scan of the controllers directory, but for now this is perfect.

  2. Sindre says:

    Thanks man, you just saved my life!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>