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.
Comment | File | Size | Author |
---|---|---|---|
#34 | 3003713-34.patch | 9.07 KB | alexpott |
#34 | 33-34-interdiff.txt | 3.63 KB | alexpott |
#33 | 3003713-33.patch | 9.16 KB | alexpott |
#24 | 3003713-24.patch | 9.62 KB | idebr |
#24 | interdiff-16-24.txt | 1002 bytes | idebr |
Comments
Comment #2
zann1e CreditAttribution: zann1e at FFW commentedPlease use this patch for canonical URLs to have absolute path if you don't want to use Metatag.
Comment #3
zann1e CreditAttribution: zann1e at FFW commentedComment #4
zann1e CreditAttribution: zann1e at FFW commentedFixed patch with applying.
Comment #5
timmillwoodWe'll need a test to make sure this works.
Comment #7
longwaveComment #8
Greg Sims CreditAttribution: Greg Sims as a volunteer commentedI believe getting Drupal in sync with Google on the canonical tag format is likely a Major priority.
Comment #9
idebr CreditAttribution: idebr at ezCompany commentedClosed #2990249: Canonical URLs in page templates should be absolute as a duplicate issue.
Comment #10
Wim LeersWe 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?
Comment #11
Greg Sims CreditAttribution: Greg Sims as a volunteer commentedSummary: 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:
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:
This is Highlighted in the Google documentation.
Comment #12
Greg Sims CreditAttribution: Greg Sims as a volunteer commentedComment #13
Wim LeersIf 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.Comment #14
Wim LeersAlso, 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.Comment #15
Wim LeersComment #16
Wim LeersComment #17
Wim LeersClosed #2738373: Canonical link should be absolute as a duplicate of this.
Comment #20
idebr CreditAttribution: idebr at ezCompany commentedWhile 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.
Comment #21
Wim LeersI 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.Comment #22
idebr CreditAttribution: idebr at ezCompany commentedAh 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!
Comment #23
Wim Leers👍 — perhaps you want to fix that last test failure? :)
Comment #24
idebr CreditAttribution: idebr at ezCompany commentedFixed the test failure in
\Drupal\Tests\node\Functional\NodeViewTest::testLinkHeader()
Comment #25
Wim LeersGreat! Now all we need is for somebody to RTBC :)
Comment #27
Dries CreditAttribution: Dries commentedI'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.
Comment #28
Wim LeersHah, thanks for the RTBC, Dries! Nice to see you here! I guess you ran into this with your personal site as well?
Comment #29
Dries CreditAttribution: Dries commentedThat's correct!
Comment #30
alexpottIs 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.
Comment #31
BerdirIt'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.
Comment #32
Wim LeersCR written: https://www.drupal.org/node/3024321.
Comment #33
alexpottThis 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.Comment #34
alexpottRather than ArrayPIs let's use
->setAbsolute()
Comment #35
alexpottCommitted 11dab2c and pushed to 8.7.x. Thanks!
Crediting @Greg Sims for creating the issue and several comments.
Comment #37
alexpottComment #38
alexpottI'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.
There might a non-minimal consequence.
[Edit: fixed branch thanks @idebr]
Comment #39
idebr CreditAttribution: idebr at ezCompany commentedI assume alexpott in #38 means this issue is committed to 8.7.x only, rather than 8.6.x.
Comment #40
nod_Change record was indeed useful, thanks :)
Comment #41
Wim Leers@nod_ Glad to hear that!
Comment #42
dwwNote: 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
Comment #44
handkerchiefIs 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