Custom Routes Order

Comments for “Custom Routes Order”

Posted by disco_2_disco on Monday 6th May 2024 at 22:43 GMT

I assumed the custom routes were applied in order - i.e. if a match was found the process stopped.

I had an issue this evening which I could not understand. Finally I moved the custom route to the end of the file and it worked.

Is this correct?

The url was: http://websiteaddress/eandwhmi/search/dsjhdjsdh

$routes = [
    'tg-admin' => 'trongate_administrators/login',
    'tg-admin/submit_login' => 'trongate_administrators/submit_login',
    'trongate-pages' => 'trongate_pages/index',
    'about' => 'page/about',

    'eandwhmi/(:any)/church%20pages/(:any)' => 'eandwhmi/redirect/church/$1/$2',
    'hmri/index.php' => 'eandwhmi/redirect/search',
    'nbp/Church_Pages/(:any)' => 'nbp/redirect/church/$1',
    'nbp/index.htm' => 'nbp/redirect/index',
    'nbp/General/(:any)' => 'nbp/redirect/general/$1',

    'eandwhmi/(:any)/(:any)/(:num)' => 'eandwhmi/parish/$1/$2/$3',
    'eandwhmi/(:any)/(:any)' => 'eandwhmi/parish/$1/$2',
    'eandwhmi/(:any)' => 'eandwhmi/county/$1',
    'eandwhmi' => 'eandwhmi/index',
    'nbp/churches/(:any)/(:num)' => 'nbp/church/$1/$2',
    'nbp/churches/(:any)' => 'nbp/church/$1',
    'nbp/churches' => 'nbp/index',
    'nbp' => 'nbp/index',

//moved from above last section to below - and all is working now
    'eandwhmi/search/(:any)' => 'eandwhmi/search/$1',
    'eandwhmi/search' => 'eandwhmi/search'

define('CUSTOM_ROUTES', $routes);

This comment was edited by disco_2_disco on Monday 6th May 2024 at 22:45 GMT

Level One Member


User Level: Level One Member

Date Joined: 18/11/2022

Posted by DaFa on Wednesday 8th May 2024 at 13:57 GMT

Hey there,

It appears that the custom routes are indeed applied in the order they are defined. If you follow the logic in 'attempt_add_custom_routes()
function attempt_add_custom_routes($target_url) {
    //takes a nice URL and returns the assumed_url
    $target_url = rtrim($target_url, '/');
    $target_segments_str = str_replace(BASE_URL, '', $target_url);
    $target_segments = explode('/', $target_segments_str);

    foreach (CUSTOM_ROUTES as $custom_route => $custom_route_destination) {
        $custom_route_segments = explode('/', $custom_route);
        if (count($target_segments) == count($custom_route_segments)) {
            if ($custom_route == $target_segments_str) { //perfect match; return immediately
                $target_url = str_replace($custom_route, $custom_route_destination, $target_url);
            $abort_route_check = false;
            $correction_counter = 0;
            $new_custom_url = rtrim(BASE_URL . $custom_route_destination, '/');
            for ($i = 0; $i < count($target_segments); $i++) {
                if ($custom_route_segments[$i] == $target_segments[$i]) {
                } else if ($custom_route_segments[$i] == "(:num)" && is_numeric($target_segments[$i])) {
                    $new_custom_url = str_replace('$' . $correction_counter, $target_segments[$i], $new_custom_url);
                } else if ($custom_route_segments[$i] == "(:any)") {
                    $new_custom_url = str_replace('$' . $correction_counter, $target_segments[$i], $new_custom_url);
                } else {
                    $abort_route_check = true;
            if (!$abort_route_check) {
                $target_url = $new_custom_url;
    return $target_url;
When a URL is matched with a custom route, the corresponding destination is substituted, and the process continues until all routes are checked. If the custom route that matches your URL is defined later in the array, it will take precedence over previous routes.

Moving the problematic custom route to the end of the array likely resolved the issue because it ensured that other more specific routes were checked first before attempting to match the URL against that particular route.

So, yes, your understanding is correct. Custom routes are applied in the order they are defined, and if a match is found, the process stops. Moving the custom route to the end of the file, as you did, is a valid approach to ensure that it is only considered after other routes have been checked.
Founding Member


User Level: Founding Member

Date Joined: 30/11/2018