Hreflang Tag Generator and Validator
Operator: Abdulla Al Fahad. How tools are built and checked
Hreflang tells a search engine which version of a page is meant for which language and region, so the French visitor lands on the French page and the British one on en-GB. The idea is simple. The upkeep is not: every page in a group has to list every other version and itself, the codes have to be exactly right, and one missing return link makes the pair worthless. A site with five languages and a hundred pages is 500 URLs, each carrying five tags: 2,500 tags that must stay consistent.
This tool builds the tags from a table, so you describe each page once and get the full set for every member. You can output HTML link tags for the head, xhtml:link entries for an XML sitemap, or HTTP Link headers for files that have no head, such as PDFs. A second mode reads tags you already have, from a page source or a sitemap, and reports what is wrong. It checks form and consistency; it cannot tell you whether search engines have picked the alternates up. For the sitemap file itself, including limits and splitting, see the XML sitemap generator on this site.
Key features
- A grid with one row per page cluster and one column per hreflang code, or a CSV in a wide layout (a column per language) or a long layout (cluster, hreflang, url, optional canonical)
- Three outputs: HTML link tags per cluster or per page, an XML sitemap with xhtml:link, and one HTTP Link header per URL
- Code checks against 184 ISO 639-1 language codes and 249 ISO 3166-1 alpha-2 country codes, with script subtags such as zh-Hant accepted
- Fixes for common mistakes: UK for GB, en_GB with an underscore, three-letter codes such as eng, jp for ja, a country code used alone, and wrong case
- Checks for duplicate codes in a cluster, a URL shared between clusters, mixed hosts and mixed http and https, x-default placement, and canonical URLs that differ from the hreflang URL
- A validator for pasted head tags, several pages at once, or a sitemap, with self-reference and return-link checks
Code rules, the three output formats and what the validator checks
A hreflang value is a language code, optionally followed by a region: en, de, en-GB, pt-BR. The language is a two-letter ISO 639-1 code in lower case and the region a two-letter ISO 3166-1 alpha-2 code in upper case, joined by a hyphen. A region on its own is not valid, and neither is a country where a language belongs: the language of the United Kingdom is en, the region is GB, so the tag is en-GB, and UK is not a country code. The special value x-default names the page to show when no listed language fits, usually a language selector or the main version. The tool also accepts a script between language and region, as in zh-Hans or zh-Hant, and notes that Google's own examples show only language and language-region.
Every page in a cluster must carry the full set of alternates, including a self-referencing entry. If the English page lists the French page, the French page must list the English one, which is called a return link. When a return link is missing, search engines may ignore the annotation for that pair. The generator produces the complete set for each cluster, so the return links exist by construction; the validator checks them in pasted pages, and says how many alternates fall outside the pasted set and could not be checked.
Hreflang should use canonical URLs. If a page's canonical link points elsewhere, hreflang on it is ignored, so the generator, given a canonical for a URL in the CSV, warns when it differs from the URL you listed. One URL can belong to one cluster only. If you put the same URL in two clusters, the second use is flagged and left out of the sitemap and headers, because the two sets of alternates would contradict each other.
The three outputs carry the same information. HTML tags go in the head of every page in the group. The sitemap form puts one url entry per page with an xhtml:link for each alternate, which keeps markup out of the pages and suits large sites. The HTTP header form is for non-HTML files; it is one Link header per URL with entries separated by commas, and a long header is flagged as it grows, since servers often cap total header size at around 8 KB. Pick one method for a given set of pages rather than mixing them.
How it works
Each cell is trimmed and its column code is checked once. A cell with an unusable code or URL is left out with a message, and the rest of the row still produces output. Codes are written in canonical case, so en-gb becomes en-GB. The x-default entry is moved to the end. The validator reads link tags with a small attribute parser, never by inserting your text into the page, so pasted markup cannot run or affect the tool.
Typical uses
- Producing the tag block for the home, pricing and about pages in en, en-GB, de and fr, plus an x-default
- Converting a translation-management export into a sitemap with xhtml:link entries
- Checking a live page's head for a wrong region code, a missing self-reference or a missing return link
- Generating Link headers for translated PDF downloads that have no HTML head
- Auditing a migration where URLs changed and some alternates still point at the old ones
Building one
- Name the language columns in the grid, such as en, en-GB, de and x-default, or open a CSV.
- Add one row per page and paste the URL of each language version; leave a cell empty when a language is missing.
- Read the Checks panel and fix errors, then choose HTML tags, XML sitemap or HTTP Link headers and copy or download the output.
- To check existing markup, switch to Validate and paste the head tags, one page per ### URL line, or a sitemap.
Example
One cluster and one pasted check
Row Pricing: en https://www.example.com/pricing, en-GB .../uk/pricing, de .../de/preise, fr .../fr/tarifs, x-default .../pricing
Pasted: the English page lists en, fr, en-UK and x-default; the French page lists only fr and deGenerated block and validator findings
5 tags for the Pricing cluster, written once and placed in the head of all 4 pages (en and x-default share one URL):
<link rel="alternate" hreflang="en-GB" href="https://www.example.com/uk/pricing" />
...
Validator: error, hreflang="en-UK": UK is not a country code, write en-GB
error, the English page lists the French page, which does not list it back
info, the French page has no x-defaultTips for best results
- Choose the x-default deliberately: a language selector or your main-market page. It is optional, so leave it out if neither applies.
- Use a language-only code (de) when one German page serves all German-speaking countries, and a language-region code (de-CH) only when you have a separate page for that market.
- Put every alternate on an absolute https URL with the exact form of the canonical, including the trailing slash or its absence.
- If you maintain tags by hand, regenerate the whole cluster when you add a language. Adding the new page's tags but forgetting to add it to the others is the commonest cause of missing return links.
Things to check before you rely on it
Hreflang is a hint, not a command. Search engines can still show a different version, especially when the content of two versions is nearly identical.
Machine-readable correctness is not the same as being indexed. A tag set can be perfect and still be ignored if the pages are blocked, redirected or not canonical.
The validator checks only the pages you paste. Return links to pages outside the paste are reported as unchecked, not as correct.
The code lists cover ISO 639-1 and ISO 3166-1 alpha-2. Languages that have only a three-letter code are not valid hreflang values, and they are rejected rather than guessed.
Common errors and how to fix them
- "UK is not a country code"
- Write GB for the United Kingdom, so en-GB. Lowercase uk is the language code for Ukrainian, which is a different thing.
- "Missing return link"
- Add the first page's URL to the alternates of the page it links to. With the generator this does not happen, because every page in the cluster gets the full set.
- "No self-reference"
- A page must list its own URL among its alternates. Add the entry for the page's own language to its tag set.
- "... is also used in another cluster"
- A URL belongs to one cluster. Remove it from one row, or merge the two rows if they describe the same page.
Best practices
- Generate the set from one source table every time instead of editing individual pages.
- Keep a canonical tag on every page that points to itself, so hreflang and canonical agree.
CSV layouts
The wide layout has a header such as cluster,en,en-GB,de,fr,x-default and one row per page cluster, with the URL of each version in its column. The first column may be omitted, in which case clusters are labelled from their first URL.
The long layout has one row per URL, with columns for the cluster, the hreflang code and the URL, and optionally a canonical column. Headers may be named cluster, group, page, id or key; hreflang, lang, language, locale or code; and url, href, loc or link. If a cluster lists the same code twice, the first URL is kept and the duplicate is reported.
Frequently asked questions
Do I need x-default?
No. It is optional. It is useful for visitors whose language you do not list, and it can point at a language selector or at your default-language page. Only one URL per cluster can be x-default.
Can I use hreflang in the sitemap instead of the head?
Yes. The sitemap form carries the same information as the HTML tags. Use one method for a set of pages rather than both, so there is a single place to update.
Why is a three-letter code like eng rejected?
Hreflang uses the two-letter ISO 639-1 codes. The three-letter form is a different standard. The tool suggests the two-letter equivalent where it knows one.