Skip to content

Canonical URL: What It Is and How to Add the Tag

A canonical URL is the main address of a page that can be opened at more than one. You name it with one line in the page’s <head>: <link rel="canonical" href="https://www.cafe.com/menu">. Every copy of the menu, like /menu?utm_source=newsletter or /menu?sort=price, then tells search engines to list https://www.cafe.com/menu instead. People can still open all of them.

The canonical tag
<link rel="canonical" href="https://www.cafe.com/menu">

What is a canonical URL?

Search engines keep track of addresses, not pages. Say your cafe has one menu page. To you, the addresses below all open that same menu. To Google they’re separate pages that happen to have the same text, and it has to choose one to show in results. “Canonical” is the word for the one that gets chosen.

Address What’s different Usual fix
https://www.cafe.com/menu?utm_source=newsletter A tracking tag added to the link in your email Canonical
http://www.cafe.com/menu http instead of https 301 redirect on the server
https://cafe.com/menu No www 301 redirect on the server
https://www.cafe.com/menu/ A slash on the end Redirect to one style, or a canonical
https://www.cafe.com/menu?sort=price Same dishes, cheapest first Canonical
https://www.cafe.com/menu/print A print version with no pictures Canonical
https://www.cafe.com/vegan/carrot-cake The carrot cake page, also listed under /cakes/carrot-cake Canonical on both, pointing at one

Having copies like these is normal, and you won’t be punished for them. The cost is quieter. Links and shares get spread across several addresses instead of adding up on one, and the address Google shows might be the long one with ?utm_source=newsletter on the end. A canonical tag lets you make that choice yourself.

The parts of an address, like the bit after ?, are explained one by one in What is a URL.

How to add the canonical tag

It’s a <link> tag, and it goes inside the <head> with the title and the other meta tags. Here’s the head of the menu page:

menu.html, the head
<head>
  <meta charset="utf-8">
  <title>Menu | Corner Cafe</title>
  <meta name="description" content="Coffee, cakes and soup, with prices.">
  <link rel="canonical" href="https://www.cafe.com/menu">
</head>
  • rel="canonical" says what kind of link it is: “this is my main address”. Nothing shows on the page.
  • href is that main address, written out in full with https:// and the domain. More on why below.
  • <link> has no closing tag. <link rel="canonical" href="…" /> with a slash means the same thing, so use whichever your other tags use.
  • One canonical per page. Two different ones and search engines may ignore both.
  • The <head> only. Google doesn’t read a canonical that ends up in the <body>, and that can happen without you moving it, see the mistakes list.

Put the same line on every copy that you control, so /menu/print carries href="https://www.cafe.com/menu" too. If the server sends a different file for the print version, that file needs its own copy of the tag. A template that prints the current address (like the one further down) would make /menu/print point at itself, so set copies like that by hand.

A PDF or other file: the Link header

A PDF has no head to put a tag in. If your menu PDF can be downloaded from two addresses, the server can send the canonical as an HTTP header with the file instead. Google reads it the same way:

An HTTP response header
Link: <https://www.cafe.com/menu.pdf>; rel="canonical"

How you add a header depends on your server or host. For ordinary web pages, the tag in the head is easier to see and to check.

A page that points at itself

The main page should carry a canonical too, pointing at its own address. It looks like it does nothing:

A self-referencing canonical
<!-- In the head of https://www.cafe.com/hours -->
<link rel="canonical" href="https://www.cafe.com/hours">

Here’s why it’s worth having. When someone opens https://www.cafe.com/hours?fbclid=IwAR0x from a social app, the server sends exactly the same HTML as for /hours, tag included. So the copy you never planned for already says “list https://www.cafe.com/hours”. One line on the real page covers every tracking link anyone will ever make to it.

Every page on this site does this. View the source of this page and you’ll find <link rel="canonical" href="https://htmlviewer.tech/tutorials/canonical-url"> near the top.

Use a full URL, not a relative one

Full address: means one thing
<link rel="canonical" href="https://www.cafe.com/menu">
Relative: depends on where it’s read
<link rel="canonical" href="/menu">

Google does accept href="/menu". It works it out like any relative link, from the address the page was opened at. That’s the catch: the address the page was opened at is the thing that varies. Opened as http://cafe.com/menu, the relative canonical becomes http://cafe.com/menu, the very copy you wanted to fold away. If a test copy of your site ends up on another host, its relative canonicals point at that host instead of your real one.

So write the whole thing, https:// and the domain included. Google recommends full URLs here, and our Meta Tag Generator won’t write a relative one.

A hint, not a command

Google treats your canonical as a strong signal, but not the only one. It also looks at redirects, which address is in your sitemap, which one your own links use, and whether it’s https. When these disagree with the tag, it may pick another page. Bing works in a similar way. You get the page you asked for most reliably when everything agrees:

  • Your menus and buttons link to https://www.cafe.com/menu, not to /menu/ or the http version.
  • sitemap.xml lists only the canonical addresses.
  • http goes to https, and one www style goes to the other, with a 301 on the server.
  • The pages really are the same. A canonical from the carrot cake page to the lemon tart page will most likely be ignored, because the content doesn’t match.

What “canonicalized” means, and what Search Console says

When /menu/print points its canonical at /menu and Google agrees, the print page is canonicalized: it’s treated as a copy of /menu and left out of results. That’s what you wanted, not an error. If you use Google Search Console, its Page indexing report sorts these pages under a few labels:

Search Console says What it means What to do
Alternate page with proper canonical tag This copy points at another page and Google went along with it. Nothing.
Duplicate, Google chose different canonical than user You named one page, Google picked another. Line up the links, sitemap and redirects with your tag.
Duplicate without user-selected canonical Google found copies with no canonical tag and chose one itself. Add a canonical if it chose the wrong one.

For one page, paste its address into URL Inspection at the top of Search Console. It shows a “User-declared canonical” (your tag) and a “Google-selected canonical” (the one it went with).

Canonical, 301 redirect or noindex?

These three get mixed up a lot because they all deal with pages you don’t want in search results. They do quite different things:

Tool People Search engines Use it for
Canonical tag Open both pages as normal Usually list only the main one and count links to the copies toward it Copies that should stay, like ?sort=price or a print page
301 redirect Are sent on to the new address Swap the old address for the new one A page that moved, http to https, one www style
noindex Open the page as normal Leave it out of results Pages nobody should land on from search, like the “Thanks for your order” page

If the old address has no reason to exist any more, redirect it: how to set up a 301. A noindex is one meta tag in the head:

Keep a page out of search results
<meta name="robots" content="noindex">
  • Don’t put noindex and a canonical to another page on the same copy. One says “drop me”, the other says “count me toward that page”. Pick the canonical for copies of a real page.
  • Don’t block copies in robots.txt to fix duplicates. A blocked page can’t be read, so the canonical tag inside it is never seen.

A canonical to another site

The href doesn’t have to be on your own domain, but for republished articles Google now prefers something else. Say a local news site republishes your post about cold brew. Google’s advice is for them to add <meta name="robots" content="noindex"> to their copy, so only your original shows in results. A canonical pointing back at your post is still allowed:

On the other site’s copy
<!-- In the head of the copy on the news site -->
<link rel="canonical" href="https://www.cafe.com/blog/cold-brew-at-home">

Either way they have to add it, since it goes in their HTML, so ask for it when you agree to the repost. Their page has its own header, other stories and ads around your text, so to Google the two pages aren’t identical and a canonical often isn’t followed. That’s why noindex is the safer ask.

Moving your own site to a new domain is a different job. Use 301 redirects for that, not canonicals.

Mistakes that get a canonical ignored

  1. Pointing at a page that redirects or is missing. A canonical to /old-menu, which redirects to /menu or returns a 404, sends search engines the long way round or nowhere. A canonical should name the final page that opens with a normal 200.
  2. Two canonical tags. Often the theme adds one and an SEO plugin adds a second. If they name different pages, Google may ignore both. Search the source for canonical and expect one match.
  3. The tag lands in the body. Writing it below </head> is the obvious way. The sneaky way is a tag that isn’t allowed in the head, sitting above it:
    The canonical ends up in the body
    <head>
      <title>Menu | Corner Cafe</title>
      <img src="visit-counter.gif" alt="">
      <link rel="canonical" href="https://www.cafe.com/menu">
    </head>
    The browser closes the head as soon as it reaches the <img>, so the canonical after it is treated as part of the body. Inspect the page in developer tools and you’ll see it there. Keep the canonical near the top of the head, above anything a plugin or a tracking snippet might add.
  4. Every page pointing at the home page. It usually comes from a template with a fixed href="https://www.cafe.com/". That claims your menu, your opening hours and your blog are all copies of the home page. Google will most likely spot that it’s wrong and ignore the tags, so none of your pages gets the benefit.
  5. Pages 2, 3 and 4 pointing at page 1. The second page of your blog lists different posts, so it isn’t a copy. Give each page its own canonical:
    Page 2 points at page 2
    <!-- On https://www.cafe.com/blog?page=2 -->
    <link rel="canonical" href="https://www.cafe.com/blog?page=2">
  6. Tracking bits inside the canonical. If the template builds the href from the full address the visitor opened, you get href="https://www.cafe.com/menu?utm_source=newsletter", which defeats the point. Build it from the path only. The URL Parser shows you what’s left once the tracking is taken out.
  7. The wrong http or www. A canonical to http://cafe.com/menu on a site that redirects everything to https://www.cafe.com points at an address that only redirects. Use the exact address your redirects end on.

How to check a page’s canonical URL

  1. Open the page and press Ctrl+U (Cmd+Option+U on a Mac) to see its source. More ways in how to view the source of a page.
  2. Press Ctrl+F (Cmd+F) and search for canonical.
  3. You want one match, inside the head, with a full https:// address that opens the right page.

View Source shows the HTML the server sent. A tag that a script adds later only shows up in Inspect, which is another reason to write it into the HTML (more on that below). To see what the browser ended up with, paste this into the developer tools Console (F12):

Developer tools, Console
document.querySelectorAll('link[rel="canonical"]').forEach((l) => console.log(l.href));

It prints each canonical as a full address, even a relative one. More than one line means more than one tag. Nothing printed means there isn’t one. From a terminal, curl fetches the page and grep finds the line:

In a terminal
curl -s https://www.cafe.com/menu | grep -i canonical

Or copy the page’s head into the Meta Tag Generator with Import from HTML. It fills in the canonical next to the title and description and tells you if the address isn’t a full one. To see the canonical Google actually picked, use URL Inspection in Search Console (above).

Setting it in a template or CMS

On a real site you don’t type the tag into every page. Put it once in the shared head template, joining your one chosen domain and each page’s own path. In PHP that can be a single line:

header.php, inside the head
<link rel="canonical" href="https://www.cafe.com<?= htmlspecialchars(strtok($_SERVER['REQUEST_URI'], '?')) ?>">

strtok(…, '?') keeps the path and drops everything from the ? on, so tracking links get the clean address. It still repeats whatever path was asked for, /menu/ included, so pair it with a redirect for the trailing slash or write each page’s path by hand.

  • WordPress adds a canonical to posts and pages by itself, and SEO plugins let you change it per page. Don’t add a second one in the theme.
  • Next.js takes it from the page’s metadata. Set metadataBase so it comes out as a full address:
    app/menu/page.js
    export const metadata = {
      metadataBase: new URL('https://www.cafe.com'),
      alternates: { canonical: '/menu' },
    };
  • Site builders usually have a “canonical URL” field in each page’s SEO settings. Leave it empty unless you want that page to point somewhere else.

Whatever you use, make sure the tag is in the HTML the server sends, not added by a script after the page loads. Google can read a canonical added with JavaScript, but only after it runs the page, and if the HTML says one thing and the script says another, you’re back to two different answers.

Questions people ask

What is a canonical URL?

It’s the one address you want search engines to show for a page that can be opened at several. https://www.cafe.com/menu, https://www.cafe.com/menu?utm_source=newsletter and http://cafe.com/menu can all show the same menu. The canonical URL is the one you pick as the main address, usually the plain https one with no extra bits after a ?.

How do I add a canonical tag in HTML?

Put one link tag inside the page’s <head>: <link rel="canonical" href="https://www.cafe.com/menu">. The href is the main address, written out in full with https:// and the domain. Use one tag per page. On a site with templates, add it once to the shared head template and build the href from each page’s own path.

Does every page need a canonical tag?

It isn’t required, but it’s a good habit. A page whose canonical points at itself (a self-referencing canonical) covers all the copies you didn’t plan for, like links with ?utm_source= or ?fbclid= added by emails and social apps. Those copies are served the same HTML, so they carry the same tag and point back to the clean address.

What does canonicalized mean?

A page is canonicalized to another page when its canonical tag points there, or when a search engine decides on its own that the other page is the main one. In Google Search Console, a copy that points elsewhere and that Google agreed with shows as “Alternate page with proper canonical tag”. That’s not an error, it’s the tag doing its job.

Should the canonical URL be absolute or relative?

Absolute. Google accepts a relative href like /menu, but it works it out from the address the page was opened at, and that address is often the very copy you’re trying to fold away, such as the http version. A full address like https://www.cafe.com/menu means the same thing on every copy.

What is the difference between a canonical tag and a 301 redirect?

A 301 redirect sends people and search engines to the new address, so the old one stops opening. A canonical tag only talks to search engines: both pages keep working for people, and search engines are asked to list just one. Redirect a page that has no reason to exist any more, and use a canonical for copies that should stay, like /menu?sort=price or a print version.

Why did Google choose a different canonical than mine?

The tag is a strong hint, not a command. Google also looks at redirects, which address is in your sitemap, which one your own links use, whether it’s https, and whether the two pages really have the same content. If those point somewhere else, it may pick that page. Make every signal agree, then check again with the URL Inspection tool in Search Console.

Can a canonical URL point to another website?

Yes, a cross-domain canonical is allowed, but for republished articles Google prefers noindex. If a news site republishes your blog post, Google’s advice is for them to add a robots noindex tag to their copy so only your original shows in results. A canonical pointing at your original also works sometimes, but their page has its own layout and other content, so Google often doesn’t follow it. Either way the other site has to add it, since it goes in their HTML.

Should a canonical URL have a trailing slash?

Either style is fine, as long as you pick one and use it everywhere. /menu and /menu/ are two different addresses, so write the canonical, your links and your sitemap the same way, and redirect the other style to it on the server if you can.

Keep going