Hide Drupal internal pages

30/04/2019 - 17:41
Average: 3.7 (3 votes)

How to quickly hide Drupal 7s automatic internal /node & /taxonomy/term/ pages without even hitting Drupal itself.

Drupals /node and /taxonomy/term/* pages

Out of the Drupal provides various automatic helper pages that lists content for you, for example lists of promoted nodes, content associated with taxonomy terms, files, etc. For extremly simple sites these pages might be usefull but for the more complicated sites you really don't need them and should hide them. Why ? Usually they are not styled in any way and might list content that shouldn't even be visible, think container nodes.

Nodes: The /node endpoint

When you go to a Drupal site and append /node to the URL you will get a nice list of all content that have the "Promote to Frontpage" flag set.

Various solutions

There are various solutions to this problem, some Drupal based, some not. For example, one way is a very simple module (For example: Nice frontpage, Rabit hole, Node page disable) that removes /node from the routing. Or use redirect module to redirect to your frontpage. Or even Rules. These of course all work, but they all hit the whole Drupal stack for no good reason just to do a redirect or some other response code.

The absolute fastest way is to handle these kind of URLs even before Drupal is even hit. In this we can use the mod_rewrite module in Apache.

Fastest solutions, use Apache mod_rewrite

The fastest way to handle these is to use Apache mod_rewrite to redirect or block these requests. Make sure your Drupal installation is configured to use aliased paths, but if not, remove the middle rule (in italic). It will only catch URLs in the format /node/NID but won't block the edit (/node/NID/edit) & delete (/node/NID/delete) operations.

Edit .htaccess in your Drupal root and add the following lines:

# Hide node listing page
RewriteRule "^node$" / [L,R]

# Hide ugly /node/xyz URLs
RewriteRule "^node/([0-9]+)$" / [L,R]

# Hide file views /file/xyz URLs 
RewriteRule "^file/([0-9]+)$" / [L,R]

# Hide node listing by taxonomy term
RewriteRule "^taxonomy/term/(.*)" / [F]

Add them just after what looks like this:

  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  # RewriteBase /

The rules will redirect /node and /node/anything to the root. Taxonomy term content listings will be blocked with a 403 Forbidden.

Now those automatic and somewhat useless URLs are nicely hidden from the world.


20/04/2018 - 11:30
Drupal, Apache, mod_rewrite