To add hreflang tags in Astro, configure i18n in astro.config.mjs, then render one <link rel="alternate" hreflang="..." href="..."> per language version of the page in your head component, plus an x-default. The getAbsoluteLocaleUrl() helper from astro:i18n builds each URL from the locale and the page path, so the links follow your routing settings. Every language version must list all the others, including itself.
Key Takeaways
- hreflang links must be absolute and reciprocal: each version lists every version, itself included.
getAbsoluteLocaleUrl(locale, path)builds correct URLs from youri18nconfig andsite.- Add
x-defaultfor the page to show when no language matches, usually the default locale.- Only list languages where the page actually exists; never point hreflang at a 404.
The i18n config
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://www.example.com',
i18n: {
locales: ['en', 'es', 'de'],
defaultLocale: 'en',
routing: {
prefixDefaultLocale: false,
},
},
});
With prefixDefaultLocale: false, English pages live at /about/ and Spanish at /es/about/. Our i18n routing guide covers the folder structure and the other routing options.
A hreflang component
Pages pass in their path without the locale prefix and the list of locales they exist in:
---
// src/components/Hreflang.astro
import { getAbsoluteLocaleUrl } from 'astro:i18n';
interface Props {
path: string; // e.g. 'about/'
locales: string[]; // locales this page exists in
}
const { path, locales } = Astro.props;
const defaultLocale = 'en';
---
{locales.map((locale) => (
<link rel="alternate" hreflang={locale} href={getAbsoluteLocaleUrl(locale, path)} />
))}
{locales.includes(defaultLocale) && (
<link rel="alternate" hreflang="x-default" href={getAbsoluteLocaleUrl(defaultLocale, path)} />
)}
On /es/about/, this renders:
<link rel="alternate" hreflang="en" href="https://www.example.com/about/" />
<link rel="alternate" hreflang="es" href="https://www.example.com/es/about/" />
<link rel="alternate" hreflang="de" href="https://www.example.com/de/about/" />
<link rel="alternate" hreflang="x-default" href="https://www.example.com/about/" />
The English and German pages render the same four lines, which makes the set reciprocal. Check that the generated URLs match your trailingSlash setting, so they do not redirect.
Pages that exist in only some languages
Blog posts are often translated selectively. Keep a shared key in frontmatter, for example translationKey: 'pricing-guide', and look up which locales have that key when building the page:
---
// src/pages/[...slug].astro (excerpt)
import { getCollection } from 'astro:content';
const { entry } = Astro.props;
const siblings = await getCollection(
'blog',
({ data }) => data.translationKey === entry.data.translationKey
);
const locales = siblings.map((s) => s.data.lang);
---
Pass locales to the component. A post that only exists in English then gets no hreflang tags at all, which is correct.
Region codes and sitemaps
hreflang accepts a language (es) or a language plus region (es-MX). Use regions only when you have region-specific content, such as prices in different currencies. Your locale codes in the i18n config can be mapped to region codes with the codes form of locales if needed.
You can also declare alternates in the sitemap instead of, or as well as, the head. @astrojs/sitemap writes them when given an i18n option:
// astro.config.mjs (sitemap excerpt)
sitemap({
i18n: {
defaultLocale: 'en',
locales: { en: 'en-US', es: 'es-ES', de: 'de-DE' },
},
});
The sitemap tip covers the rest of the setup, and the config docs show where i18n goes in a theme project. Any theme in our catalog can be made multilingual this way, since the routing is plain Astro.
FAQ
Does Astro add hreflang tags automatically?
No. The i18n config handles routing and gives you URL helpers, but you render the <link rel="alternate"> tags yourself, or let @astrojs/sitemap add alternates to the sitemap.
Should hreflang include the current page?
Yes. Each language version should list every version, including itself. Search engines may ignore hreflang sets that are not reciprocal.
What is x-default in hreflang?
It names the page to show users whose language does not match any listed version. Most sites point it at the default-language page or a language picker.
Do I need hreflang in both the head and the sitemap?
No, one method is enough. The head is easier to debug per page; the sitemap is easier for very large sites. Avoid conflicting values if you use both.