3

I need to redirect users to an external website when they access the node view page at node/%node of a node meeting some conditions.

What would the proper/best way to achieve this (using code) be?

2

I would use hook_node_view.

Remember to check the view mode along with the conditions, so you don't redirect on pages where only the teaser is shown.

It should look something like this:

module_node_view($node, $view_mode, $langcode) {
  if ($view_mode == 'full' && "Conditions") {
    drupal_goto('http://example.com');
  }
}
  • Is it documented/accepted that the 'full' view mode should only be used when the node is displayed by itself as a page? In Drupal 6, I would to it in hook_nodeapi when $op == 'view' and $a4 == TRUE. – Pierre Buyle Apr 8 '11 at 12:28
  • I found node_is_page($node) to add as condition to ensure the node is displayed itself as a page before redirecting to an external site. This should also protected against redirection during content indexation on cron. – Pierre Buyle Apr 11 '11 at 10:18
0

We created a content type using CCK Redirection and then external links could be treated like nodes. So you could use node access modules, the rules module, etc. It has worked quite well for us.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.