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

Created on 20 August 2011, almost 13 years ago
Updated 3 May 2024, about 2 months ago

Problem/Motivation

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.

Steps to reproduce

TBD

Proposed resolution

TBD (may still be the same but should be confirmed)

Remaining tasks

Tests
Review

User interface changes

TBD

API changes

Data model changes

Release notes snippet

The logic of the AliasPathProcessor has been changed to return / when requesting the URL of the frontpage instead of an empty string.

Original Post

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

11.0 πŸ”₯

Component
RoutingΒ  β†’

Last updated 2 days ago

Created by

πŸ‡¨πŸ‡³China olbion

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

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.69.0 2024