Option 1: Using the Rules module
Using the Rules module you can implement a rule that looks similar to this:
{ "rules_check_url" : {
"LABEL" : "Disallow node/* access",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "init" : [] },
"IF" : [
{ "text_matches" : {
"text" : [ "site:current-page:url" ],
"match" : "node\/\\d+$",
"operation" : "regex"
}
}
],
"DO" : [
{ "drupal_message" : {
"message" : "Sorry, URLs like [site:current-page:url] are not allowed around here ...",
"type" : "error"
}
},
{ "redirect" : { "url" : "no_access" } }
]
}
}
The above rule does not (yet) take into account to only apply the "action" for anonymous users only. But for anybody a bit familiar with the Rules module, that is a straight forward "Condition" to add ...
To experiment with this rule in your own site, just copy the entire Rules code above, and paste it in a new Rule in your own site, created via the "Import" function. Then further edit/refine to make it fit in your own environment (e.g the "Sorry, ..." message to be shown).
Option 2: Using theRabbit Hole module
Consider using the Rabbit Hole module. Here is an excerpt from its project page:
... providing multiple options to control what should happen when the entity is being viewed at its own page. You have the ability to
- Deliver an access denied page.
- Deliver a page not found page.
- Issue a page redirect to any path or external url.
- Or simply display the entity (regular behavior).
This is configurable per bundle and per entity. There is also a permission that lets certain roles override Rabbit Hole completely.
Advice:
From both options provide above, I'd pick the Rules based solution ... Either because you might already have Rules installed, or if you have to still install it you'll discover tons of other use case to also be resolved by the same Rules module (possibly complemented with the Flag module soon ...)