I'm currently using the Redirect module in Drupal 8 along with Pathauto to create aliases on all url's. So for example /node/23 would be /article/some-text-here. When a user types in /node/23 in the url it automatically redirects them to the alias url. I want to show a 404 whenever the user uses the node path url instead of redirecting that way they don't ever have a chance to find something they shouldn't by traversing through my site with that pattern.
3 Answers
The rabbit_hole module can be used to set 'access' controls for rendered nodes, taxonomies, and other entities. When a user visits the /node/* or /taxonomy/term/* page, you can control what happens--403 page, 404 page, redirect, whatever. This can also be overridden on a per-node basis if you require that.
https://www.drupal.org/project/rabbit_hole
This still allows the entities to be accessible via path alias and shouldn't break any references to those nodes/taxonomies.
Make sure apache mod_alias
is enabled and create a rule in .htaccess
like:
Redirect 404 /node/.*$
-
this had no effect. I tried RewriteRule ^/?node/[^a-zA-Z_]+$ redir_path [L,R=301] and it worked but I want to display the 404 instead of redirecting.– klo009Feb 16, 2017 at 21:43
I would also try this, although this would apply for Drupal 7
$path = current_path();
if($path == 'node/23') {
drupal_not_found();
}
-
This is not a good answer - original question is asking for a scalable solution, not hardcoded. Also, this won't work for Drupal 8.– KevinFeb 17, 2017 at 14:54
-