A public article can appear through a category path, an old slug, a query variant, or a print view. Choose one canonical route for the main content and make alternate routes redirect or declare their relationship consistently. The canonical target must represent substantially the same page; it is not a way to point thin or unrelated pages at a stronger one. Generate the target from trusted route data, not arbitrary query text. A stable title, description, and initial HTML content help readers and crawlers understand the page before optional scripts run. A canonical hint is a signal, not a substitute for working links and response codes.
Canonical Route Identity
Working case
The inspection guide moves from '/guides/old-valve-check' to '/guides/valve-inspection'. Existing bookmarks should reach the new guide through a permanent redirect. The new page uses its own stable canonical location; pagination or filter links remain distinct when they lead to meaningfully different content. A preview draft should not be published under the same canonical path as a live page. The code builds the canonical location from a trusted site origin and route map. The server should emit it in initial HTML alongside a useful page title.
Implementation
function canonicalForGuide(siteOrigin, guideSlug) {
const allowedSlugs = new Set(["valve-inspection", "pump-review"]);
if (!allowedSlugs.has(guideSlug)) throw new Error("Unknown guide route");
return new URL(`/guides/${guideSlug}`, siteOrigin).href;
}
const canonicalLocation = canonicalForGuide(process.env.PUBLIC_SITE_ORIGIN, "valve-inspection");Cost and boundaries
One redirect adds a network round trip for visitors who use an old path. Maintaining a route map costs O(R) entries for R historical redirects, so consolidate chains and remove loops. Canonical metadata itself is small, but inconsistent values across server and client renders create difficult diagnostics. Test direct access, redirects, status codes, and final path in the build. A site with many duplicate routes wastes crawl and maintenance work; stable route ownership makes future migrations easier.
Common Mistakes
- Do not point unrelated pages at one canonical route.
- Do not build a redirect chain when one hop will do.
- Do not set a client-only canonical different from initial HTML.
Connected lessons
Content Discovery and Structure; Structured Data from Visible Facts; Sitemap and Index Control; Internal Link Architecture for Technical Guides; Page loading: keep content available while CSS and scripts arrive; HTML document skeleton: declare language, encoding, and a real title; Release checks: prove the critical route and prepare a rollback.
Failure trace
An old guide slug, a new category slug, and a query variant all return 200 with the same article. Internal links alternate between them, so updates and shares fragment across several identities. Choose the main route, redirect obsolete routes when practical, and keep canonical metadata aligned with the final URL. Do not point a genuinely different article at this route merely to consolidate signals.
Verification
- Follow an old URL and confirm its redirect reaches the intended current page without a loop.
- Compare the canonical target with the final response URL and the links rendered in the page.
- Test a query variant that changes actual content; decide whether it is a distinct page.
Decision note
A canonical hint is weaker than a consistent site graph. Fix internal paths and server responses first, then use metadata to describe the same identity to automated readers.
Apply and check
Build Project: release and recovery drill for a content service; then check the boundary with Web Development: offline and delivery contracts quiz.
