Search code examples
regexcodeigniterroutesclean-urls

Custom CodeIgniter route regex


Trying to improve a custom codeigniter route regex that I have created. Essentially the purpose of the custom route is to create a cleaner/shorter URL for client profile pages which have the format of clients/client-slug, for example: clients/acme-inc. I only want this route to match if their are no additional segments after the client-slug segment, and if they client-slug value does not match any of the 'reserved' values which correspond to actual methods/routes in the Clients controller. Currently, this is what I'm using:

$route['clients/(?!some_method|another_method|foo|bar)(.+)'] = 'clients/index/$1';

This mostly works ok, except for when there is a client-slug that begins with one of the reserved methods text, i.e. clients/food-co, which since it has clients/foo in it, the custom route is not matched. So I need to basically conditionally allow the route to contain any of the reserved methods in that set ONLY IF it is followed by additional characters (that is not a /).


Solution

  • Do you try this?

    $route['clients/(?!(?:some_method|another_method|foo|bar)(?:/|$))(.+)'] = 'clients/index/$1';