Skip to main content
← πŸ“„ HTML & the platformΒ·Module A6 Β· Lesson 9
TaskAdd hreflang link tags for a 4-language page: en (/post), uk (/uk/post), ru (/ru/post), fr (/fr/post), plus x-default (/post). All absolute URLs starting with https://example.com.

hreflang: 18 languages, one URL hierarchy

100 XP8 min
Theory

When you have translated pages

Tell Google "here are sibling versions of this page in other languages, route the user to the right one":

<link rel="alternate" hreflang="en" href="https://example.com/post" />
<link rel="alternate" hreflang="uk" href="https://example.com/uk/post" />
<link rel="alternate" hreflang="ru" href="https://example.com/ru/post" />
<link rel="alternate" hreflang="x-default" href="https://example.com/post" />

Rules

  • Every translated page links to ALL siblings (including itself). If you have 18 langs, you have 19 link tags (18 siblings + 1 x-default) on EVERY page.
  • hreflang values are BCP 47: en, uk, en-US, pt-BR, zh-Hans, x-default.
  • `x-default` = the fallback when the user's language doesn't match any explicit hreflang. Usually points to your English URL.
  • Bidirectional confirmation. The UK page must link back to the EN page. If the link is one-way, Google ignores both.

Why it matters

Without hreflang, Google indexes both /post and /uk/post as separate competing pages. They cannibalize each other's rankings. With hreflang, Google knows they're the same content for different audiences β€” keeps both indexed, shows the right one based on user locale.

Sitemap alternative

For sites with many translations, listing hreflang in the XML sitemap (one entry per URL with embedded alternates) is more maintainable than emitting 19 link tags per page.

CodeMentor uses both

Our buildHreflangLanguages() helper on every page emits the 18 link tags, AND the sitemap includes hreflang for each URL. Belt + suspenders. Google reads either.

πŸ”’

Sign up to start coding

Theory is open to everyone. The interactive editor, live preview, and check are unlocked with a 7-day free trial β€” card required, cancel anytime.

Sign up β€” free trial β†’

First 10 lessons in each track are free. No card needed for those.

← PreviousNext lesson β†’

Get one Python or web tip a day β€” by email

Short, hand-written, no spam. Unsubscribe in one click.