Do canonical links require a full domain?
Asked Answered
C

7

70

I want to add canonical links to my pages, but do I need to specify the domain, or will a relative URL do?

In other words, is:

<link rel="canonical" href="/item/1">

good enough, or do I need to use:

<link rel="canonical" href="http://mydomain.com/item/1">
Conte answered 11/12, 2011 at 22:12 Comment(1)
See the Webmaster Stack Exchange take on this question: Using relative URLs in <link rel="canonical" … />Fonz
M
66

Directly from Google:

http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394

Can the link be relative or absolute?
rel="canonical" can be used with relative or absolute links, but we recommend using absolute links to minimize potential confusion or difficulties. If your document specifies a base link, any relative links will be relative to that base link.

Mcentire answered 11/12, 2011 at 22:17 Comment(6)
link and href were defined by Tim Berners Lee in the first specs of HTML. Google are no more "direct" than anyone else with an opinion.Testate
Fair enough, but since most people but the canonical in because of google, i figured the answer from google is what counts for them.Mcentire
Yep. They're a good source, but not (excuse the pun) a canonical source.Testate
Google has changed that page and no longer states that relative URLs are accepted (afaics). Instead they write "Avoid errors: use absolute paths rather than relative paths with the rel="canonical" link element.". Does that mean that (root) relative canonical tags are no longer ok? In that case what are the downsides and reasons for this? Is there any "canonical" source stating that (root) relative canonical tags are ok?Simplism
@Simplism My guess is that that text is merely suggesting use of absolute URLs for error avoidance, not declaring that relative URLs are errors. If one uses a relative URL on any page that can be reached from multiple domains, then a relative URL is an error since you now have multiple canonical URLs pointing to the same document.Willmert
It looks like Google is pushing absolute URLs more over the years. You can see the progression from the original answer (recommend absolute to minimize potential confusion), to Qtax's comment in 2016 (to avoid errors use absolute), and today this is what Google states: "Use absolute paths rather than relative paths with the rel="canonical" link element." They give an example: Not this structure: /dresses/green/greendress.html.Moiety
K
9

Again, Google says this:

https://support.google.com/webmasters/answer/139066?hl=en

Avoid errors: 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).

For example’s sake, these are their URLs:

http://example.com/wordpress/seo-plugin/

http://example.com/wordpress/seo/seo-plugin/

This is what rel=canonical was invented for. Especially in a lot of e-commerce systems, this (unfortunately) happens fairly often, where a product has several different URLs depending on how you got there. You would apply rel=canonical in the following method:

You pick one of your two pages as the canonical version. It should be the version you think is the most important one. If you don’t care, pick the one with the most links or visitors. If all of that’s equal: flip a coin. You need to choose.
Add a rel=canonical link from the non-canonical page to the canonical one. So if we picked the shortest URL as our canonical URL, the other URL would link to the shortest URL like so in the <head> section of the page:

<link rel="canonical" href="http://example.com/wordpress/seo-plugin/">

That’s it. Nothing more, nothing less.

Kindergartner answered 25/2, 2016 at 15:20 Comment(1)
There is no difference between http://example.com/wordpress/seo-plugin/ and /wordpress/seo-plugin/ when on http://example.com. The question is: is the full URL is required in the canonical tag, if so why? What is wrong with a root relative URL like /wordpress/seo-plugin/?Simplism
T
6

All href attributes are hypertext references - that's what it stands for. As such, they are always URI-References, not URIs, and can be relative.

In this case though, there's a benefit in putting in the full URI if you can, because it will survive anything that migrates it onto another domain in the future (assuming you will still want the domain listed to be the canonical one), and can even survive some of the cruder automated plagiarisms :)

That benefit is pretty slight if you aren't actively using non-canonical versions on other domains though, so I wouldn't expend much effort on it.

Testate answered 11/12, 2011 at 22:34 Comment(0)
A
6

There is nothing special about canonical. It’s a standard link type, for use with standard ways to provide links (e.g., the link element), so you can specify any kind of URL reference (absolute, relative, protocol-relative, in combination with the base element, empty, …).

RFC 6596 (The Canonical Link Relation) explicitly says:

The target (canonical) IRI MAY:

One of the examples:

[…] or as a relative IRI:

<link rel="canonical" href="page.php?item=purse">
Andrus answered 9/6, 2017 at 13:16 Comment(0)
V
0

Update on canonical best practices: rel="canonical" has cross-domain support. Google's source: https://webmasters.googleblog.com/2009/12/handling-legitimate-cross-domain.html

Moreover, the introduction of structured data makes the use of canonicals even more strict, as Google will not pick-up the JSON markup from not canonical sources (a mistake I happen to have made!).

Votive answered 27/9, 2017 at 15:36 Comment(1)
This is false: "Google will not pickup JSON markup from non-canonical sources."Raki
R
0

Relative canonical paths are accepted. This one works best:

<link rel="canonical" href="#"/>

It points to the current document's URL – including queries – sans the hash part.

Raki answered 7/3, 2022 at 4:29 Comment(0)
T
-1

If you only have one domain for that website, is ok to use the absolute path: <link rel="canonical" href="/item/1">

Tem answered 18/7, 2019 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.