The canonical link element on the front page seems to be set incorrectly if the front page is set to something other than default. If the front page is set to say, 'node/200', then the canonical link element on the home page will be 'node/200' instead of the base domain, which it should be IMO.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

idevai’s picture

Same here. Actually I don't know how to fix this using the admin interface.

What I did was something like this in template.php:

if (request_uri() == '/page-used-as-front-page' || request_uri() == '/') {

    drupal_add_html_head_link(array(
     'rel' => 'canonical',
      'href' => '/',
    ));
}

Hope this helps.

dmitrii’s picture

Version: 7.x-1.3 » 6.x-1.4

Thank for your code.
my template.php

function MYTHEME_html_head_alter(&$head_elements) {
  if (drupal_is_front_page()) {
    foreach ($head_elements as $key => $element) {
      if (isset($element['#attributes']['rel']) and ($element['#attributes']['rel'] == 'canonical' or $element['#attributes']['rel'] == 'shortlink') and $element['#attributes']['href'] != '/') {
        unset($head_elements[$key]);
      }
    }
  }
}

if (drupal_is_front_page()) {
  drupal_add_html_head_link(array(
    'rel' => 'canonical',
    'href' => '/',
  ));
}
friendlymachine’s picture

Thanks to both of you for posting a workaround.

artfulrobot’s picture

Thanks for fixing this dumb behaviour which was (amongst other things) breaking my Tweet button (which was picking up the node/123). It's good to have a canonical URL because it provides clarity. No good if that "clarity" is wrong/confusing!

smitty’s picture

I think this should be fixed in the Global Redirect module. As soon as you switch on the »Frontpage Redirect Handler« to redirect any request to the frontpage path to the site root the canonical URL (inserted by Drupal Core) must be deleted or set to the site root.

miro_dietiker’s picture

Priority: Normal » Major
Issue summary: View changes

IMHO: This is a pretty major thing.

In case the front page is set to a node, the code for page_node_view adds the node/ID canonical element.
https://api.drupal.org/api/drupal/modules%21node%21node.module/function/...

drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);

In that case, formally, the front page represents the node. This is why core can claim, setting it to canonical node/ID_FRONT is not wrong.

Now, global redirect adds a redirect header to the node/ID_FRONT page, resulting in a circular redirect. This is fatal.
Since a week now, facebook stops counting likes / shares for such pages. (Before i haven't seen this issue.)

Facebook offers a developer tool to resolve header + open graph tags.
https://developers.facebook.com/tools/debug/
If you add your front page URL here, you will see the fatal error:

Critical Errors That Must Be Fixed
Could Not Follow Redirect Path
Using data from http://example.com because there was an error following the redirect path.

And we ended in trouble with blocked and resetted sharing counters. See here how to query the records: http://stackoverflow.com/questions/5699270/how-to-get-share-counts-using...

Resulting in this conclusion:
If open graph forces node/ID_FRONT to redirect to front page, it needs to fix canonical of at least front page too. Possibly also node/ID_FRONT.

miro_dietiker’s picture

Additionally providing the screen of the Facebook URL debugger error.
Error output