use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\node\NodeInterface;
function MYMODULE_preprocess_page(&$variables) {
if (isset($variables['node']) && \Drupal::currentUser()->isAnonymous()) {
$node = $variables['node'];
if ($node instanceof NodeInterface) {
if ($node->getType() === 'your_node_type') {
$path = \Drupal::request()->getPathInfo();
if (preg_match('#/node/\d+/*$#', $path)) {
throw new NotFoundHttpException();
}
}
}
}
}