Add canoncial & redirect to a specific page for a node type

Public

This code could be useful for node types that are just used to build a list
Just replace MY_MODULE, MY_NODE_TYPE, MY_REDIRECT_PATH with your own data

Get raw version
php
  1. function MY_MODULE_node_view($node, $view_mode, $langcode) {
  2. if ($node->type == 'MY_NODE_TYPE' && $view_mode == 'full'){
  3.  
  4. // Add canonical tag
  5. global $language;
  6. if (isset($language->prefix) && !empty($language->prefix)){
  7. $redirect_url = '/' . $language->prefix . '/MY_REDIRECT_PATH';
  8. }
  9. else{
  10. $redirect_url = '/MY_REDIRECT_PATH';
  11. }
  12. $canonical = array(
  13. '#tag' => 'link',
  14. '#attributes' => array(
  15. 'rel' => 'canonical',
  16. 'href' => $redirect_url,
  17. ),
  18. );
  19. drupal_add_html_head($canonical, 'canonical');
  20.  
  21. // Create a redirect rule for MY_NODE_TYPE node full view
  22. drupal_goto('MY_REDIRECT_PATH');
  23. }
  24. }