Free auditStart free
GuideNext.js · Canonical URLs

Next.js canonical tags

Set canonical URLs in Next.js App Router metadata, handle dynamic routes and locales, and verify the canonical Google receives.

3 min
read
6
sources
Oct 24, 2026
updated
Scaplet Editorial Team
author
In one line

In the Next.js App Router, set canonical URLs with metadata.alternates.canonical. Define metadataBase in the root layout so relative URL values resolve to the production host, then provide a page-specific canonical for dynamic routes. Inspect the deployed HTML for one absolute canonical that matches redirects, internal links, and sitemap URLs. Google treats canonicals as signals and can choose another URL.

30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01Checklist

Set a canonical base in the root layout

Next.js documents metadataBase as the base URL for metadata fields that need an absolute URL. It is typically defined in the root app/layout.js, where it applies to URL-based metadata in that route segment and below. Replace the example host with the production canonical host; a preview domain in this value can publish the wrong preference across many routes. Next.js shallowly merges metadata from parent segments, so do not set alternates.canonical in the root layout: a route that defines no alternates of its own would inherit that value and declare itself a duplicate of it.
  1. 01An absolute canonical URL ignores metadataBase.
  2. 02A relative URL-based metadata field without a configured metadataBase can cause a build error.
  3. 03Next.js normalizes duplicate slashes between the base and relative values. It treats a leading slash in a metadata path as relative to the base rather than replacing its path.
  4. 04Google recommends absolute URLs in rel="canonical" and says relative paths, although supported, can cause problems later. metadataBase makes the rendered tag absolute.
app/layout.tsx
import type { Metadata } from "next"

export const metadata: Metadata = {
  metadataBase: new URL("https://www.example.com"),
}

export default function RootLayout({ children }) {
  return <html lang="en"><body>{children}</body></html>
}
02Check it

Generate canonical URLs for dynamic routes

A dynamic product, article, or category route should set its own canonical using its actual route data. If the page has query parameters for tracking, sorting, or selecting content, decide whether those parameters represent a distinct page before emitting a canonical. Do not automatically canonicalize every query URL to the homepage. The code below uses Next.js 15 and later, where dynamic params are asynchronous; Next.js 14 and earlier use synchronous params, so follow the docs for your installed version.

Keep the canonical URL consistent with the URL the page actually serves after redirects. Generate it from trusted route data or a fixed base, not an incoming Host header that a request can change.

Page-specific canonical (Next.js 15+)
import type { Metadata } from "next"

type Props = { params: Promise<{ slug: string }> }

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { slug } = await params
  const item = await getPublishedItem(slug)

  return {
    alternates: {
      canonical: `/guides/${item.slug}`,
    },
  }
}
Sources · 6

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • nextjs.org
  • google.com
  1. 01generateMetadata | Next.js Documentationnextjs.org · checked 2026-10-24
  2. 02generateMetadata | Next.js 14 Documentationnextjs.org · checked 2026-10-24
  3. 03generateMetadata | Next.js 15 Documentationnextjs.org · checked 2026-10-24
  4. 04How to specify a canonical with rel=canonical and other methods | Google Search Centraldevelopers.google.com · checked 2026-10-24
  5. 05What is canonicalization | Google Search Centraldevelopers.google.com · checked 2026-10-24
  6. 06Create and submit a sitemap | Google Search Centraldevelopers.google.com · checked 2026-10-24
Questions

Asked a lot.

3 questions people ask next, answered from the sources on this page.

01How do I add a canonical URL in Next.js?

In the App Router, export metadata with alternates.canonical. Set metadataBase in the root layout if you want relative canonical paths to resolve to the production host.

02Why is Next.js generating a wrong canonical host?

Check metadataBase in the root layout and confirm it is set to the preferred production host rather than a preview or request-derived domain.

03Does a canonical tag force Google to use that URL?

No. Google treats canonical annotations as signals and may choose another URL based on redirects, internal links, sitemap entries, and page content.

Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane