$eventName = $modx->event->name;
if ($eventName != 'OnPageNotFound') return;

// Get last alias from url (preserve only the alias after last slash). Needed when using subfolders.
$alias = basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); 
$path_parts = pathinfo($alias);
$resource = $modx->getObject('modResource', array('alias' => $path_parts['filename']));

if ($resource) {
    $error_page = $modx->getOption('error_page');
    $modx->sendForward($error_page, 'HTTP/1.1 410 Gone'); // ID found > 410
}