url() should return / when asked for the URL of the frontpage

Created on 20 August 2011, over 13 years ago
Updated 30 March 2022, almost 3 years ago

I've been very confused with the addition of canonical links in Drupal 7. The default behaviour seems to be to assign the actual node url of the node being displayed on the front page as the canonical uri. But that must be incorrect, since going to that uri will only redirect to the front page. So I've made a few changes to two files:

common.inc, line 2143, before:

  if ($path == '<front>') {
    $path = '';
  }

changed to:

  if ($path == '<front>' || $path == variable_get('site_frontpage', 'node')) {
    $path = '';
  }

Now the front page has "/" as canonical url. Also, other tags like < meta about ..> get updated.

Once I got going, I made two other changes as well in node.module, line 2592:

  $uri['options']['absolute'] = TRUE;
  // Set the node path as the canonical URL to prevent duplicate content.
  $canonical_href = url($uri['path'], $uri['options']);
  drupal_add_html_head_link(array('rel' => 'canonical', 'href' => $canonical_href), TRUE);
  // Set the non-aliased path as a default shortlink.
  $shortlink_href = url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)));
  if(strlen($shortlink_href) < strlen($canonical_href)) {
    drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => $shortlink_href), TRUE);
  }

This does two things:
1. Sets canonical links as absolute urls, which seems better if site can be accessed from different urls. Should perhaps be an option somewhere?
2. Only adds the shortlink IF its url is actually shorter than the aliased path. It seems weird to say that the shortlink of "/" is "/node/12345", or that another shortlink of "/abc" is "/node/34567".

What do you think about these changes? I think it's crucial SEO-wise to get it right. I'm not an SEO expert but I've asked around a bit and this seems to be along the right tracks. I hope that the Drupal core can be updated some way to be more optimized for SEO and web standards.

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
RoutingΒ  β†’

Last updated 8 days ago

Created by

πŸ‡¨πŸ‡³China olbion

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024