17

How can I remove these tags from my pages in Drupal 7?

<link rel="shortlink" href=" .... " />
<meta name="Generator" content="Drupal 7 (http://drupal.org)" />
<link rel="canonical" href="...." />

Update: I have the code for template.php, which removes the generator and canonical tags.

function program_html_head_alter(&$head_elements) {
  unset($head_elements['system_meta_generator']);
  foreach ($head_elements as $key => $element) {
    if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
      unset($head_elements[$key]); 
    }
  }
}

Can anybody add something to remove the short link tag?

1
  • 1
    Everything looks good, but why take canonical out? This is actually helpful.
    – user1760
    Jun 26, 2011 at 21:11

3 Answers 3

10

This is displayed in html.tpl.php in the variable $head. $head is added to the variables in template_process_html by using drupal_get_html_head(). In that function, you can see that hook_html_head_alter() is called.

There can you remove these.

However, note that these links can be important for SEO, to avoid that a search engine thinks that you have duplicate content on your site, if for example a node is available through node/nid and the alias.

4
  • hmm i find it but still can't remove it :) I think i should learn php soon :) I find code which can be add to template.php (in template) and the code remove "Generator" line. function program_html_head_alter(&$head_elements) { unset($head_elements['system_meta_generator']); }
    – user332
    Mar 10, 2011 at 8:50
  • but i have still problem with 2 additional lines which i need to remove. It is not good for seo if you have custom homepage and canonical give url not for main domain only for subpage... the same with shortlink - why somebody can see that article has additional adress with /node/...stupid idea to add these lines to d7
    – user332
    Mar 10, 2011 at 8:57
  • So if I have a fresh D7 installation with clean URLs and pathauto, the canonical URL link will be automatically set?
    – Andy
    Mar 27, 2011 at 10:18
  • Should be, yes.
    – Berdir
    Mar 27, 2011 at 14:49
5

I use the following function within my template.php file:

/**
 * Used to remove certain elements from the $head output within html.tpl.php
 *
 * @see http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_html_head_alter/7
 * @param array $head_elements
 */
function YOUR_THEME_NAME_html_head_alter(&$head_elements) {
    $remove = array(
        'system_meta_generator',
        'metatag_canonical',
        'metatag_shortlink'
    );

    foreach ($remove as $key) {
        if (isset($head_elements[$key])) {
            unset($head_elements[$key]);
        }
    }

    // Use this loop to find out which keys are available.
    /* -- Delete this line to execute this loop
    echo '<pre>';
    foreach ($head_elements as $key => $element) {
        echo $key ."\n";
    }
    echo '</pre>';
    // */
}
1
  • This didn't work for me - 'metatag_canonical and 'metatag_shortlink do not exist as indexes of $head_elements (this is in Drupal 7). The answer given by @Levente worked fine.
    – user34185
    Apr 30, 2015 at 12:44
4

I solved shortlink too, modyfying your code.

Someone could tidy it up properly, because I don't know how to put it properly, and neither if it has side effects in this way or not. It makes the unwanted links disappear from the head, though.

function nameof_mytheme_html_head_alter(&$head_elements) {
  unset($head_elements['system_meta_generator']);
  foreach ($head_elements as $key => $element) {
    if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
      unset($head_elements[$key]);
    }

    if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'shortlink') {
      unset($head_elements[$key]);
    }
  }
}

Yes, it's a butcher's attitude towards canonical links, but until no better sulotion comes up, this is still better.

THAT'S WHY: you need canonical links shown ONLY on the duplicate pages, the ones that are not the preferred version. Original pages should be left alone: none canonical should be showing there! That's why I still butcher around.