Problem/Motivation

Drupal 8 creates <link rel="canonical" href="/relative/url" /> by default. support.google.com says:

Use absolute paths rather than relative paths with the rel="canonical" link element.

Use this structure: https://www.example.com/dresses/green/greendresss.html Not this structure: /dresses/green/greendress.html

I used the Google Search Console URL Inspection and found Google does not recognize relative urls as canonical. It says:

User-declared canonical N/A

This caused some google indexing issues with our D8 Multi-Language website.

Please see also: https://drupal.stackexchange.com/questions/270150/link-rel-canonical-hre...

Proposed resolution

Use absolute links for link relations

Remaining tasks

User interface changes

Link relations are now absolute.

API changes

N/a

Data model changes

N/a

Release notes snippet

Link relations now use absolute URLs because Google doesn't support relative URLs.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Greg Sims created an issue. See original summary.

zann1e’s picture

Please use this patch for canonical URLs to have absolute path if you don't want to use Metatag.

zann1e’s picture

Status: Active » Needs review
zann1e’s picture

Fixed patch with applying.

timmillwood’s picture

Issue tags: +Needs tests

We'll need a test to make sure this works.

Status: Needs review » Needs work

The last submitted patch, 4: canonical_absolute_path-3003713-3.patch, failed testing. View results

longwave’s picture

Greg Sims’s picture

Priority: Normal » Major

I believe getting Drupal in sync with Google on the canonical tag format is likely a Major priority.

idebr’s picture

Wim Leers’s picture

Status: Needs work » Postponed (maintainer needs more info)
Related issues: +#2406533: edit-form, delete-form etc. <link> tags added on /node/{node} are invalid according to W3C Validator

We made this pass the W3C validator in #2406533: edit-form, delete-form etc. <link> tags added on /node/{node} are invalid according to W3C Validator.

Is this no longer passing that validator?

Greg Sims’s picture

Summary: RFC 6596 (The Canonical Link Relation) says you can use either absolute or relative urls. W3C tests to this specification. Google highlights a Best Practice of using an absolute url in their documentation. Google and other search engines are using the information specified in this tag. I suggest we follow their Best Practice recommendations of the only people who use this information -- the search engines.

Details follow:

W3C tests to the specification. RFC 6596 (The Canonical Link Relation) says:

The target (canonical) IRI MAY:

Specify a relative IRI (see [RFC3986], Section 4.2).

Google promoted the use of rel="canonical" in conjunction with Microsoft and Yahoo. Google now has cross-domain support for the tag. They updated their Best Practices to include:

Use absolute paths rather than relative paths with the rel="canonical" link element.

Use this structure: https://www.example.com/dresses/green/greendresss.html
Not this structure: /dresses/green/greendress.html

This is Highlighted in the Google documentation.

Greg Sims’s picture

Status: Postponed (maintainer needs more info) » Active
Wim Leers’s picture

Issue tags: +D8 cacheability

If we do this, a consequence is that every single "view single entity page" with this link will have to vary by the url.site cache context.

Wim Leers’s picture

Status: Active » Needs review
FileSize
1.33 KB

Also, if we're doing this for rel=canonical, we might as well do it for all of them. Consistency is important. And since cacheability will already be negatively impacted if any one of them results in an absolute URL, there is no extra impact to do it for them all.

Wim Leers’s picture

Version: 8.6.1 » 8.7.x-dev
Wim Leers’s picture

Wim Leers’s picture

Related issues:

Closed #2738373: Canonical link should be absolute as a duplicate of this.

The last submitted patch, 14: 3003713-14.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 16: 3003713-16.patch, failed testing. View results

idebr’s picture

Also, if we're doing this for rel=canonical, we might as well do it for all of them. Consistency is important.

While consistency is important, let's not forget we are also increasing the bytes in the head of the document prior to the body. This means we are adding length to the critical rendering path. Personally, I am not sure if the added bytes are worth it.

Wim Leers’s picture

Personally, I am not sure if the added bytes are worth it.

I applaud you for thinking about this. I wish more people did so! 👏

However … the rel=canonical link will already have caused this cost, anything after it that also uses it is pretty much "for free", since 99.99% of web servers have gzip enabled.

idebr’s picture

Ah yes, I had not considered gzip abstracting the absolute URL to reduce the file size of the request. This solves my objection in #20, thanks Wim!

Wim Leers’s picture

👍 — perhaps you want to fix that last test failure? :)

idebr’s picture

Status: Needs work » Needs review
FileSize
1002 bytes
9.62 KB

Fixed the test failure in \Drupal\Tests\node\Functional\NodeViewTest::testLinkHeader()

Wim Leers’s picture

Great! Now all we need is for somebody to RTBC :)

Dries’s picture

I've tested this patch and it works as expected.

Despite the W3C recommendation, Google believes canonical URLs should be absolute: see https://github.com/GoogleChrome/lighthouse/issues/3178 for a Google Lighthouse discussion. Drupal's SEO friendliness matters so I'd make this change.

Wim Leers’s picture

Hah, thanks for the RTBC, Dries! Nice to see you here! I guess you ran into this with your personal site as well?

Dries’s picture

That's correct!

alexpott’s picture

Category: Bug report » Task
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

Is this worth a change record? I think so - in case anything else is adding link relations in contrib / custom then they have a chance to learn about this too. Also I'm not sure this is a bug per se. Nothing is broken we are failing to comply with a Google recommendation but there are many recommendations that we do not comply with.

Berdir’s picture

It's more than a recommendation if you're using AMP because that does require canonical to be absolute or it fails validation.

Not really worth arguing about, +1 on doing a change record.

Wim Leers’s picture

Title: <link rel=“canonical” href=“/relative/url” /> href should be absolute » Use absolute URLs in link relations Google doesn't support relative URLs
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record
alexpott’s picture

This patch conflicted with #3019834: Add @trigger_error() to deprecated url/link EntityInterface methods in core/modules/node/tests/src/Functional/NodeViewTest.php I rebased and rerolled the patch.

alexpott’s picture

Rather than ArrayPIs let's use ->setAbsolute()

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 11dab2c and pushed to 8.7.x. Thanks!

Crediting @Greg Sims for creating the issue and several comments.

  • alexpott committed 11dab2c on 8.7.x
    Issue #3003713 by Wim Leers, alexpott, zann1e, idebr: Use absolute URLs...
alexpott’s picture

Issue summary: View changes
alexpott’s picture

I've published the change record. I've only committed to to 8.7.x since I'm being cautious as to how the following ends up playing out.

This means we sacrifice some cacheability on Drupal sites that are exposed on multiple domains. The consequences of that are minimal though.

There might a non-minimal consequence.

[Edit: fixed branch thanks @idebr]

idebr’s picture

I assume alexpott in #38 means this issue is committed to 8.7.x only, rather than 8.6.x.

nod_’s picture

Change record was indeed useful, thanks :)

Wim Leers’s picture

@nod_ Glad to hear that!

dww’s picture

Note: for anyone else who needs it, patch #14 in here applies cleanly to 8.6.x and solves the problem (but doesn't fix the tests). So, you can patch your sites with it if you want to get this working on 8.6.* sites while you wait for 8.7.0 to come out.

Enjoy,
-Derek

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

handkerchief’s picture

Is it correct that this issue/fix only affects node URLs?
If yes, why? Because there is exactly the same problem with taxonomy URLs. The taxonomy link relations are relative and therefore not SEO compliant.

#edit: Ok I've now seen the other issue ;) #3186317: Use absolute URLs for Terms in link relations Google doesn't support relative URLs