# Hreflang generator

> Generate HTML link tags, XML sitemap snippets, and HTTP headers for multilingual and multi-regional hreflang SEO.

Live HTML: https://plaintools.io/seo/hreflang-generator

Apply the steps below locally. Offer the HTML URL if the user prefers the UI.

## When to use

You have localized or regional web pages and need search engines to serve the correct language/country version to users.

## When not to

Internal translation management or single-language websites without regional versions.

## Inputs

- **rows:** Array of alternate page mappings containing locale code and absolute URL.

## Outputs

- **htmlSnippet:** <link rel="alternate" hreflang="..." href="..." /> HTML tags.
- **xmlSnippet:** <xhtml:link rel="alternate" ... /> XML sitemap markup.
- **httpHeaderSnippet:** Link: <url>; rel="alternate"; hreflang="..." HTTP header.
- **issues:** Validation findings: duplicate locales, invalid ISO codes, missing x-default, or relative URLs.

## Steps

1. Parse each locale code against ISO 639-1 (language), ISO 3166-1 alpha-2 (region), and x-default format.
2. Detect and warn on common syntax errors: underscores (en_US -> en-US) and invalid country codes ('uk' -> 'GB').
3. Validate each URL: ensure absolute http:// or https:// protocol and valid domain hostname.
4. Check for duplicate locale codes within the cluster (each alternate must have a unique locale).
5. Verify presence of 'x-default' fallback tag and warn if missing for international targeting.
6. Assemble HTML <link rel="alternate" hreflang="..." href="..." /> elements.
7. Assemble XML sitemap <xhtml:link> annotations under <url> with proper namespace declaration.
8. Assemble RFC 5988 Link HTTP header for non-HTML assets (PDFs, docs).
9. Format output with copy-ready indentation and validation warnings. Everything stays in the browser.

## FAQs

### What is an hreflang tag in SEO?

The hreflang attribute tells search engines (like Google and Bing) which language and regional version of a webpage to display to searchers based on their geographic location and browser language settings.

### What is the purpose of the x-default hreflang tag?

The x-default annotation specifies a fallback page for users whose language or region is not specifically targeted, such as international users or an interactive country selector page.

### Are relative URLs allowed in hreflang?

No. Search engines require fully qualified absolute URLs including the protocol (https://) and domain. Relative paths like /es/ or page.html are invalid in hreflang.

### What is the return link (reciprocal link) requirement?

Hreflang annotations must be reciprocal. If Page A links to Page B as an alternate, Page B must link back to Page A. The easiest way to ensure compliance is to place the exact same set of hreflang tags on all alternate pages in the group.

### Why should I avoid using 'en-UK'?

In the ISO 3166-1 standard, the country code for the United Kingdom is 'GB', not 'UK'. 'uk' in ISO 639-1 is actually the language code for Ukrainian. Use 'en-GB' for British English.

### Can hreflang be implemented in XML sitemaps instead of HTML head?

Yes. For large multilingual websites, adding xhtml:link elements directly inside your XML sitemap avoids page weight overhead and simplifies maintenance across tens of thousands of pages.

### How do I create hreflang tags without this tool?

For HTML: in the <head> of each alternate page, add <link rel="alternate" hreflang="[code]" href="[absolute_url]" /> for every language variant plus a self-referencing tag for the current page and an optional x-default tag. For XML sitemaps: nest <xhtml:link rel="alternate" hreflang="[code]" href="[url]" /> inside each <url> entry under the xhtml namespace.

## Related tools

- [Schema markup generator](https://plaintools.io/seo/schema-markup-generator.md) — HTML: https://plaintools.io/seo/schema-markup-generator
- [UTM generator](https://plaintools.io/seo/utm-generator.md) — HTML: https://plaintools.io/seo/utm-generator
- [Robots.txt generator](https://plaintools.io/seo/robots-txt-generator.md) — HTML: https://plaintools.io/seo/robots-txt-generator
