Blog Sections Open

Routing URLs Like /catalog/id Through OnPageNotFound

When a project needs cleaner product URLs without moving the data model into resources, an OnPageNotFound router can bridge the gap — as long as URL rewriting stays predictable.

This donor solved a practical routing problem: products lived in a separate table, but the site needed cleaner URLs such as /catalog/id instead of catalog.html?object=id.

Legacy approach

if($modx->event->name == 'OnPageNotFound'){
    if($modx->config['friendly_urls'] != 1) break;
    $q = explode('/', $_SERVER['REQUEST_URI']);
    switch ($q[count($q)-2]) {
        case 'object':
            $_GET['object'] = $q[count($q)-1];
            $modx->sendForward(2);
        break;
    }
}

What mattered here

the note also found that a custom rewriteUrls implementation in one distribution interfered with this flow. Reverting to the stock function made the routing work again.

Recommendation

If you route custom URLs through OnPageNotFound, keep the rewrite layer minimal and predictable. Otherwise small URL helpers can unexpectedly break your custom router.

Newer post

Fixing Mixed Database Encodings Before a Site Migration

If the site renders correctly but phpMyAdmin shows mojibake, do not migrate blindly — first identify how the data was stored and which connection settings are masking the issue.

Older post

Validating a Required TV Before Saving a Resource

If a TV must never be left empty, the save flow should stop before persistence and show the editor a clear message instead of silently accepting bad data.