How to Add Canonical Tag in HTML
How to add canonical tag in html correctly, with copy-paste examples, verification steps, and rules for canonicals vs redirects.
How to add canonical tag in html: place a rel="canonical" link element inside the page's head and point it to the preferred absolute URL. Use it on duplicate or near-duplicate pages, keep only one canonical per page, and verify it in rendered HTML, not just in your template.

If you manage duplicate content, parameter URLs, faceted pages, or syndicated copies, the canonical tag is one of the clearest consolidation signals you can send. In HTML, the tag belongs in the document head, and Google treats it as a hint rather than a command, which is why implementation and consistency both matter.
This guide focuses on the practical gaps most pages skip: copy-paste snippets, rendered-page verification, when not to use JavaScript injection, and how to audit canonicals across many URLs with YepAPI's Onpage Instant API.
What a canonical tag is in SEO#
A canonical tag tells search engines which URL you want treated as the main version of a page. The syntax is a link element with rel=\"canonical\" that points to the canonical URL.
Canonical tags matter because duplicate or near-duplicate URLs can split signals across multiple versions. Google can also choose its own canonical when your signals conflict, which is why internal linking, redirects, status codes, and page content should support the same preferred URL.
According to Schema.org's definition of canonicalUrl, a canonical URL is the URL preferred as the representative version of content. The HTML head element exists specifically for document metadata, as the WHATWG HTML specification for the head element explains.
How to add canonical tag in html#
Follow these steps in order:
- Decide which URL is the preferred version.
- Add one
rel=\"canonical\"tag inside the HTML head. - Use the full absolute URL, including protocol and host.
- Keep the canonical URL indexable and returning
200 OK. - Make internal links point to the same preferred URL.
- Verify the tag in rendered HTML and the HTTP response.
Here is the minimal HTML canonical tag snippet:
Example Page
That is the core implementation. If your page is the preferred version of itself, use a self-referencing canonical. If another URL should win, point to that exact URL instead.
When you want to inspect canonical placement quickly during an audit, YepAPI's Onpage Instant API is the direct endpoint for pulling on-page SEO elements from a live URL in code.
Canonical tag example: self-referencing page#
A self-referencing canonical is the safest default for indexable pages that should stand on their own.
Blue Running Shoes
This tells search engines that the current URL is the preferred version. Many SEO teams use self-referencing canonicals as a baseline because they reduce ambiguity when tracking parameters, mixed trailing slashes, or uppercase variants appear.
Canonical tag example: parameter URL#
Parameter URLs often produce duplicate pages. In that case, canonicalize the variant to the clean URL.
Blue Running Shoes
Applied to this page:
https://example.com/shoes/blue-running-shoes/?utm_source=email&sort=price_ascThe parameterized URL stays accessible for users, but ranking signals should consolidate to the clean canonical URL.
Canonical tag example: cross-domain canonical#
A cross-domain canonical can be appropriate for syndicated or republished content.
Guest Article Copy
Use this only when the canonical target is truly the original or preferred source. If both pages should rank independently because the content is materially different, do not force a cross-domain canonical.
How to add canonical tag in WordPress#
In WordPress, the best approach is usually to let your SEO plugin or theme output the canonical in the head. If you add one manually, make sure the plugin is not already generating a second tag.
Your main checks are simple:
- View source and search for
rel=\"canonical\". - Confirm there is only one canonical tag.
- Confirm the URL is absolute and returns
200. - Confirm the page's internal links also use that preferred URL.
If you need to review many WordPress pages at once, YepAPI's Onpage Content API can help extract page-level HTML content signals for larger QA workflows.
How to add canonical tag in html using javascript#
Avoid JavaScript injection when you can place the canonical directly in server-rendered HTML. Canonical tags belong in the head, and JS-based insertion is easier to break with hydration issues, delayed rendering, tag duplication, or framework mistakes.
A browser may show the injected tag in DevTools, but that does not make JS the best SEO implementation path. MDN's document.querySelector documentation is useful for inspection, but inspection is not the same as reliable canonical delivery.
If a framework leaves you no other option, inject exactly one tag as early as possible, then verify the rendered head and crawler output. For technical SEO work, server-side output is still the safer standard.
Canonical vs redirect vs noindex#
Use the mechanism that matches the problem. A canonical is not a substitute for every consolidation task.
| Situation | Best choice | Why |
|---|---|---|
| Duplicate URLs should remain accessible | Canonical tag | Consolidates signals while keeping variants usable |
| Old URL should send users and bots to a new URL | 301 redirect | Consolidates signals and removes the extra destination |
| Page should not appear in search results | Noindex | Tells search engines not to index the page |
| Deleted page with no replacement | 404 or 410 | Signals the page is gone |
| Near-duplicate pages that need separate rankings | Neither canonical nor redirect | Rewrite or differentiate content instead |
If you are deciding between a canonical and a redirect, start with the user outcome. If the duplicate page should never be visited as a separate destination, a redirect is usually cleaner. If users need the variant page but you want one ranking URL, use a canonical.
For a deeper redirect primer, see 301 redirect vs 302 and what is 301 redirect. If you want to validate status codes and indexability at scale, YepAPI's Domain Overview API and YepAPI's Onpage Instant API are the practical code-first tools.
How to verify a canonical tag in rendered HTML and HTTP response#
Implementation is only half the job. Verification catches template bugs, CMS conflicts, and bad deployments.
1. Check page source#
Open the page source and search for rel=\"canonical\". Confirm the tag is in the head, appears once, and points to the exact preferred URL.
This first check catches obvious failures such as missing tags, relative URLs, or the wrong environment domain.
2. Check rendered HTML in DevTools#
Open DevTools, inspect the head, and confirm the final rendered HTML still contains the same canonical. This step matters when a framework mutates the head after load.
The Chrome DevTools documentation on opening DevTools explains how to inspect the live DOM. Use this check to spot duplicate canonicals introduced by client-side scripts or plugins.
3. Check the HTTP response and final URL#
Confirm the page returns 200 OK, not a redirect loop, soft 404, or blocked destination. A canonical pointing to a non-indexable or broken page weakens the signal.
If you prefer command-line verification, a HEAD request is enough to inspect the response:
curl -I https://example.com/shoes/blue-running-shoes/The curl documentation for the -I option shows how to fetch response headers only. Pair this with source inspection so you validate both the page and its canonical target.
4. Check crawler output across many pages#
One page is easy. Hundreds are where errors hide.
For production audits, pull each page's canonical, status code, title, robots directives, and rendered content signals through YepAPI's Onpage Instant API. If you also need a general technical baseline, best on page seo analysis tools and technical seo best practices give useful context.
Common canonical mistakes#
Most canonical problems are signal conflicts, not syntax errors.
Multiple canonical tags#
A page should have one canonical tag. Multiple canonicals force search engines to choose among conflicting hints.
Canonical to a non-200 page#
Do not canonicalize to a redirecting, blocked, noindexed, or missing URL. The target should be crawlable, indexable, and stable.
Canonicalizing non-duplicate pages#
Do not canonicalize pages that should rank separately. If category pages, product variants, or localized pages serve distinct intent, a blanket canonical can suppress visibility you actually want.
Relative or malformed URLs#
Use absolute URLs. Relative canonicals can work in some cases, but absolute URLs reduce ambiguity across environments, protocols, and hostnames.
Canonical in the body#
Place the tag in the head, not the body. A misplaced tag is an implementation defect, not a best-practice debate.
Canonical conflicts with hreflang, pagination, and internal linking#
Search engines evaluate canonical signals in context. Stronger systems fail when related signals disagree.
Hreflang conflict#
If an international page points its canonical to another language version, you can undermine your hreflang setup. Each language page usually needs a self-referencing canonical unless one version is a true duplicate.
Pagination conflict#
For paginated sequences, do not point every page to page 1 unless the later pages are genuine duplicates. If page 2 and page 3 contain unique products or articles, canonicalizing them all to page 1 can collapse useful URLs.
Internal linking conflict#
If your canonical says one URL is preferred but your nav, XML sitemaps, and contextual links point elsewhere, you send mixed signals. Internal linking should reinforce the canonical URL, not fight it.
If you are cleaning up larger technical issues around canonical targets, how to do on-page seo and what is robots txt are the most relevant published reads.
How to audit canonical tags at scale with YepAPI#
Manual checks do not hold up once you have thousands of URLs. This is where programmatic auditing becomes the real solution.
With YepAPI's Onpage Instant API, you can check canonical presence, canonical target, status code, title tag, indexability clues, and page-level mismatches across many URLs. If you need to discover pages first, YepAPI's Domain Pages API is the natural companion for building the crawl list.
A practical audit workflow looks like this:
- Export or collect the URLs you want to test.
- Request each page through the on-page endpoint.
- Parse the canonical URL from the response.
- Flag pages with no canonical, multiple canonicals, or invalid targets.
- Flag canonical targets returning redirects, 4xx, or noindex.
- Compare internal link destinations against the preferred canonical pattern.
- Route fixes to the template, CMS rule, or redirect layer.
For broader issue discovery, YepAPI's Onpage Lighthouse API can support technical reviews beyond canonical tags, and YepAPI's Web Scraping API is useful when you need custom extraction from rendered pages.
CTA#
Need to validate canonical tags across hundreds or thousands of URLs? Use YepAPI's Onpage Instant API to pull on-page SEO elements, verify canonical targets, and catch conflicts in code. You get $5 free credit and no card is required.
FAQ#
What is a canonical tag example?#
A canonical tag example is a link element in the HTML head that points to the preferred page URL, such as https://example.com/page/. A common use case is a parameter URL that canonicalizes to the clean version. The page stays accessible, but search engines are guided toward the main URL.
What is a canonical example?#
A canonical example is the preferred version of a page among duplicates or near-duplicates. For instance, if both https://example.com/product and https://example.com/product?ref=email load the same content, the clean product URL is the canonical URL, and the parameter version should point to it with rel="canonical".
What is a canonical URL?#
A canonical URL is the URL you want search engines to treat as the main representative version of a page. It is used when similar content exists on multiple URLs, such as filtered pages, tracking-parameter URLs, or syndicated copies. The canonical helps consolidate ranking signals onto one preferred destination.
Why are canonical tags important?#
Canonical tags are important because they help consolidate signals from duplicate or near-duplicate URLs, reduce ambiguity about the preferred page, and support cleaner indexing decisions. They also prevent internal systems from sending mixed signals when parameters, faceted navigation, or cross-domain copies create multiple versions of essentially the same content.
Topics