How can users be prevented from accessing direct page URLs, requiring them to go through the alias? For example, 'node/5' should appear as a 404 error, but 'alias-to/fifth-node', which is aliased to that same 'node/5', would return the page. (I currently have the globalredirect and pathauto modules, but don't see any such features there.)

I'm not sure but I think I could use Apache URL rewriting to achieve this effect (right?), but if there's a way to do it in Drupal that'd be better for various reasons like customization, etc... Is this possible?

Comments

alex.skrypnyk’s picture

subscribing

el_topo’s picture

Title: How to allow direct page URL access only through alias? » Use custom_url_rewrite_inbound()

I had the same problem, and sorted out by adding custom_url_rewrite_inbound() to settings.php.

This method is better than Apache rewrite rules, as it let you prevent access to node pages depending on user authentication status, or any other condition you may want.

You will also get a nice 404 error in drupal reports page every time someone tries to access a node directly and the message will include which was the original page entered (as notfound_node/11)

The reason why I let the admin user to access node/x pages, is that he's the one that will edit pages, and for that task he needs to access node/xx/edit.

My settings.php file has the following function:


function custom_url_rewrite_inbound(&$result, $path, $path_language)
{
  global $user;

  // I do not filter url for the admin user
  if($user->uid == 1)
  {
    /* $result=$path; */ Just found out that this creates problems with URL aliases.
    return;
  }

  // I don't want people accessing pages through HOST/node/number
  if (preg_match('|^node/(.*)$|', $path, $matches))
  {
    $result='notfound_node/'.$matches[1];
    return;
  }

  // Prevent accessing main node page (HOST/node and HOST/node/)
  if (preg_match('|^node/?$|', $path))
  {
    $result='notfound_node';
    return;
  }
}

I'm not a Drupal expert but I think this works well. Hope it helps.

el_topo’s picture

VicOnt’s picture

Issue summary: View changes

Good module:
https://www.drupal.org/project/access_filter
allows deny from node/n with a +B:Blind key that means will let through aliases