On my site, some nodes are unpublished forever instead of being deleted. I'd like to return Page not found (404) for anonymous trying to access unpublished node pages instead of Access denied (403). It's not about displaying a different message to the user, but rather about returning the 404 error code (important for bots).
So I've added the following to my module:
function mymodule_init() {
global $user;
$normal_path = current_path();
$parts=explode('/', $normal_path);
$nid_part = $parts[1];
$node = node_load($nid_part);
if (!isset($node)) {
return;
}
if (($node->status == 0) &&
($node->nid == $nid_part) &&
($user->uid == 0)) {
drupal_not_found();
}
}
But I still get Access denied as anonymous user on unpublished pages. How to implement it?