Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

Should hreflang live in the HTML, the HTTP header, or only in the sitemap? Let's argue about it

Automated technical SEO checks that catch indexing blocks and deploy regressions — seotto.com If you have more than one language or country version of a page, hreflang is how you tell search engines those versions are

Automated technical SEO checks that catch indexing blocks and deploy regressions — seotto.com

If you have more than one language or country version of a page, hreflang is how you tell search engines those versions are companions, not duplicates. You can put the annotations in three places: <link rel="alternate" hreflang="..." href="..."> in the HTML, a Link HTTP header, or xhtml:link entries in the sitemap. Official guidance is: pick one. Teams pick two anyway, or pick the sitemap because the HTML is a mess, or pick the HTML and then forget a language.

Each place has a failure mode I have now seen more than once. So the argument is worth having.

Let's argue about it.

The case for HTML

You can see it. View-source, a curl, a CI diff of the rendered template: the annotations are on the document. When a product page is missing hreflang="de", a developer can find it without unzipping a sitemap. Debugging is half the job.

It travels with the page. A correct HTML block is hard to "forget" for a new URL if it is part of the same template that already knows the locales. A sitemap generator that runs on a different schedule can lag.

It is the format most examples and libraries use. Next.js, many CMSes, and most snippets assume tags in <head>. Going somewhere else means you are off the beaten path when something breaks.

The downsides: every language must be listed on every variant, including x-default if you use it, and the URLs must be absolute and consistent. A typo in one tag is easy. Pages that are not HTML (PDFs, sometimes files) cannot use this. And if the <head> is assembled badly, you can emit a different set of tags than you think — especially with client-rendered tags that Google may or may not see the way you expect.

The case for the HTTP header

Non-HTML URLs. If you actually need hreflang on a PDF or an image, the header is the mechanism. This is a smaller set of sites than the blogs imply, but when it is your set, HTML will not help.

A clean separation of concerns. Some teams prefer not to bloat <head> and already have a good edge layer. A header can be added by the same service that already knows the locale map.

The downsides are serious: headers are easy to drop at a CDN, easy to cache wrong, and much harder to eyeball. A missing or stale Link header has taken weeks to notice, in my experience, because nobody looks at response headers when they debug "the German URL is in the US index". Also, the header can get long; at enough languages you have to care about size limits.

The case for the sitemap only

The template stays simple. Locale mappings can be generated next to the URL list you already build, in one job, with tests. A sitemap is a good machine-readable map of the site; hreflang is a map, so the sitemap is a natural home.

You cannot forget a page that is already in the sitemap, if the generator is written correctly. Adding a language is a data problem, not a new partial in five templates.

The downsides: sitemaps lag, sitemaps get cached, Search Console "couldn't fetch" hides the annotations entirely, and what Google uses may be a day or a week behind a template fix. Debugging is "download the sitemap, search for this URL, check the xhtml links". People don't do that on a Tuesday afternoon. And a sitemap-only strategy does nothing for a URL that is not in the sitemap (paginated filters, or a new page the generator missed).

Where I've landed

I want HTML as the source of truth for HTML pages, generated from the same locale map the sitemap uses, and I do not want two conflicting sources.

  1. Put hreflang in the HTML for ordinary pages, server-rendered, absolute URLs, every locale including a self-reference, plus x-default if you have a default.
  2. Generate the sitemap from the same map, but I no longer also put hreflang in the sitemap if the HTML is complete. Two sources that can drift will, and Google has to pick. If you cannot put tags in the HTML (a constrained CMS, or non-HTML), then the sitemap (or the header) is the one source, and you test it like a product.
  3. Headers only for non-HTML, or for a system that truly has no HTML head you control. If you use headers, monitor them: a CI check or an external fetch that the Link values match the map.
  4. Never emit tags only in client-side JavaScript. If the server HTML doesn't have them, assume they might not count.

Hreflang breaks silently: a missing language, a typo, a sitemap that lagged a deploy. seotto.com checks pages after you ship for technical SEO regressions, so a template that dropped alternate tags or pointed them at a staging host gets caught as a change, not as a wrong-country ranking a month later.

The parts I'm still unsure about

  • Very large language sets. I have not been thrilled with 30-plus tags in <head>, and I have not been thrilled with a 30-language sitemap-only setup either. I don't have a clean line.
  • Whether a sitemap-only site of otherwise excellent HTML is really worse, or just slower to debug. Google says any one place is enough. My bias toward HTML is a debugging bias.
  • CC/locale combinations (en-GB vs en) and how often a tidy map in one place is undone by marketing adding a country microsite that never enters the map at all. That last problem is bigger than the format question.

A quick experiment

Pick a URL you care about in two languages. curl the HTML, curl the headers, and search the sitemap. Count how many places list the alternates, and whether they agree. If they disagree, you have your answer about "pick one". If they agree, you are in good shape and you can still choose to delete the extras.

Related reading

Where do you put hreflang today, and have you ever had two sources disagree about the same URL?

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.