Documentation updates · Dec 17, 2025 · 2 min read

How to set a canonical URL in HTML, not just JavaScript

Google's canonical guide says to set a canonical URL by adding a link element with rel="canonical" to the head of each duplicate page, pointing to the preferred page, and that the element counts only inside the head.

What it means for your Maryland business

To set a canonical URL in HTML, add one link element with rel=canonical to the head section of the page's code, pointing to the full web address you want Google to treat as the main version. Google accepts the element only in the head, recommends a full address over a shortened path, and suggests putting the same tag on the main page itself. If your website builder doesn't let you edit code, Google notes it may offer a search engine settings page for this instead. JavaScript, the code that runs in the browser, should leave that tag alone, or be the only place it is set when the HTML can't hold it. This matters most to Maryland businesses with custom-built or app-style websites, online stores whose filters and tracking codes create many addresses for one product, booking and ordering platforms, and real estate or auto dealer sites built on listing software.

The custom online store of an outdoor gear shop in Cumberland, in Allegany County, prints one canonical address in the HTML, then a script added by a reviews plugin rewrites it to a different version of the same product address with a tracking code attached. Google now sees two conflicting signals for every product page. The shop's developer removes the script's change, so the HTML tag stands alone, and checks that each product's tag carries the full address of the clean product page.

Conflicting signals are the core risk Google's guide describes, and JavaScript that changes the canonical is one. Others are mismatched addresses in the HTTP header and the page's tag, relative paths that can cause trouble later, and naming an address with a # fragment as the canonical. Using noindex to steer which page is chosen blocks that page from Search entirely. Also link to the main address within your own site, since linking consistently to it helps Google understand which version you prefer.

What to do this week

2 steps. Ticks are saved to Your plan on this device.

Open Your plan

01
Ask your web developer whether your canonical tags are set in the page's HTML or added by JavaScript.
02
If JavaScript touches the canonical tag, make sure it sets the same address as the HTML, or that the HTML leaves the tag out entirely.

In Google's words

“The best way to do this is to specify the canonical URL in the HTML source code and make sure that JavaScript doesn't change the canonical link element.”

Google, exact quote · How to Specify a Canonical with rel="canonical" and Other Methods · checked against this page 2026-11-28

What Google says

Google's canonical guide says to set a canonical URL by adding a link element with rel="canonical" to the head of each duplicate page, pointing to the preferred page, and that the element counts only inside the head. On December 17, 2025, Google added JavaScript advice to that guide and to its JavaScript basics: state the canonical in the HTML source and keep scripts from altering it, or, if the HTML can't carry it, leave it out there and set it with JavaScript alone.

Report an error