Back to blog
Hreflang Validator7 min readApril 18, 2026

Hreflang in XML Sitemaps: Implementation for Large Sites

For sites with thousands of pages, adding hreflang to every HTML file is impractical. The XML sitemap centralizes all configuration in one place. Learn the correct structure and how to avoid the most common mistakes.


Adding hreflang tags in the <head> of each page is manageable when you have dozens of URLs. But if you have an ecommerce with 10,000 products in three languages, or a content portal with versions for five countries, keeping the HTML tags up to date becomes a maintenance nightmare. The XML sitemap is the solution: it centralizes all hreflang configuration in a single place, easier to generate automatically and maintain.

Why use the XML sitemap for hreflang?

  • A single file (or set of files) consolidates all hreflang configuration for the site.
  • It can be automatically generated from the database or CMS without touching the HTML of each page.
  • Google discovers language variant relationships without having to crawl every page individually.
  • It simplifies audits: you can review the entire site's hreflang configuration in a single sitemap.
  • It is Google's recommended method for sites with large URL volumes.

Google accepts all three implementation methods (HTML, HTTP header, XML sitemap) equivalently. There is no ranking advantage between them — the choice is purely technical and based on maintenance considerations.

Correct XML sitemap structure with hreflang

The sitemap with hreflang uses the W3C xhtml namespace for alternate link tags. Each URL in the sitemap must declare all its language variants, including itself.

xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml">

  <!-- Spanish version -->
  <url>
    <loc>https://example.com/es/product/sneakers/</loc>
    <xhtml:link rel="alternate" hreflang="es"
                href="https://example.com/es/product/sneakers/"/>
    <xhtml:link rel="alternate" hreflang="en"
                href="https://example.com/en/product/sneakers/"/>
    <xhtml:link rel="alternate" hreflang="fr"
                href="https://example.com/fr/produit/baskets/"/>
    <xhtml:link rel="alternate" hreflang="x-default"
                href="https://example.com/es/product/sneakers/"/>
  </url>

  <!-- English version of the same product -->
  <url>
    <loc>https://example.com/en/product/sneakers/</loc>
    <xhtml:link rel="alternate" hreflang="es"
                href="https://example.com/es/product/sneakers/"/>
    <xhtml:link rel="alternate" hreflang="en"
                href="https://example.com/en/product/sneakers/"/>
    <xhtml:link rel="alternate" hreflang="fr"
                href="https://example.com/fr/produit/baskets/"/>
    <xhtml:link rel="alternate" hreflang="x-default"
                href="https://example.com/es/product/sneakers/"/>
  </url>

  <!-- French version of the same product -->
  <url>
    <loc>https://example.com/fr/produit/baskets/</loc>
    <xhtml:link rel="alternate" hreflang="es"
                href="https://example.com/es/product/sneakers/"/>
    <xhtml:link rel="alternate" hreflang="en"
                href="https://example.com/en/product/sneakers/"/>
    <xhtml:link rel="alternate" hreflang="fr"
                href="https://example.com/fr/produit/baskets/"/>
    <xhtml:link rel="alternate" hreflang="x-default"
                href="https://example.com/es/product/sneakers/"/>
  </url>

</urlset>

Bidirectionality is also required in the sitemap. Each cluster variant must appear as a <url> entry in the sitemap and include xhtml:link tags pointing to all other variants, including itself. If you only declare one direction, Google may ignore the relationships.

Specific hreflang errors in sitemaps

Error 1: Forgetting to declare the xhtml namespace

Without the xmlns:xhtml attribute on the <urlset> tag, the XML validator will reject the sitemap and Google will not be able to process the xhtml:link tags. This is the most common error when building the sitemap manually.

Error 2: Including only URLs from one language in the sitemap

If your sitemap only lists Spanish URLs and each entry includes xhtml:link tags toward English and French versions, Google has partial information. The English and French versions must also appear as <url> entries with their own xhtml:link blocks to confirm bidirectionality.

Error 3: Sitemap hreflang conflicting with HTML tags

If you implement hreflang in the sitemap AND in the HTML of the pages, but the values do not match, Google receives contradictory signals. Choose a single method and apply it consistently across the entire site.

How to organize the sitemap for very large sites

Google limits sitemaps to 50,000 URLs and 50 MB per file. For large multilingual sites, this limit is quickly exceeded. The solution is to use a sitemap index that points to secondary sitemaps organized by language or section.

xml
<!-- sitemap-index.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://example.com/sitemap-es.xml</loc>
  </sitemap>
  <sitemap>
    <loc>https://example.com/sitemap-en.xml</loc>
  </sitemap>
  <sitemap>
    <loc>https://example.com/sitemap-fr.xml</loc>
  </sitemap>
</sitemapindex>

Each secondary sitemap (sitemap-es.xml, sitemap-en.xml, etc.) contains the URLs for that language with xhtml:link tags pointing to all variants. Google processes all sitemaps in the index and builds the complete hreflang relationship map.

How to generate the hreflang sitemap automatically

In WordPress with WPML

WPML automatically generates the sitemap with hreflang if you enable the integration with whichever sitemap plugin you use (Yoast SEO, Rank Math, or WPML's native sitemap). No additional configuration is required — WPML knows the relationships between translations and includes them in the sitemap.

In Next.js with next-sitemap

javascript
// next-sitemap.config.js
module.exports = {
  siteUrl: 'https://example.com',
  generateRobotsTxt: true,
  alternateRefs: [
    { href: 'https://example.com/es', hreflang: 'es' },
    { href: 'https://example.com/en', hreflang: 'en' },
    { href: 'https://example.com/fr', hreflang: 'fr' },
  ],
}

Verify the sitemap hreflang with the validator

After generating the sitemap, extract a sample of URLs from different languages and enter them in iRankly's Hreflang Validator. Confirm that bidirectionality is correct, language codes are valid, and all cluster URLs respond with 200 OK.

Try the tool for free

Hreflang ValidatorAnalyze your URLs with {tool} by iRankly. No sign-up, no credit card.

Use tool for free

Try the tool for free

Analyze your URLs with Hreflang Validator by iRankly. No sign-up, no credit card.

Use tool for free