Sitemaps and multilingual sites (hreflang)
A sitemap gives the search engine a list of the addresses it should crawl and the time of their last change. How to generate it in PHP from a database, check it and hand it over to search engines – and how to connect the language versions of pages on a multilingual website with hreflang.
What a sitemap is for
A sitemap is an XML file with a list of page addresses. With it, the search engine finds even pages that few links lead to and recognizes which ones have changed. It helps most for large websites, new websites without links from other websites and websites where content is added often. A small website with good navigation can do without it, but it does no harm. It does not guarantee indexing – it is a hint that the search engine may or may not use.
The format
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/</loc>
<lastmod>2026-10-01</lastmod>
</url>
<url>
<loc>https://www.example.com/menu/</loc>
<lastmod>2026-09-15T10:30:00+02:00</lastmod>
</url>
</urlset>
- loc – the full address of the page. The & character in an address is written in XML as &, accented letters and spaces in the path are encoded (%C3%A1). List only the canonical addresses of pages that return 200 and do not have noindex – no redirects, copies with parameters or error pages.
- lastmod – when the page last changed significantly: a date, or a date and time with the time zone. Google uses it only when it is consistently accurate. Setting today as the date of all pages on every generation is worse than not giving it at all – the search engine learns that it cannot trust it. A change of the year in the footer is not a significant change, a new paragraph is.
- changefreq and priority – the format knows them, but Google does not use them. Feel free to leave them out.
The file must be in the UTF-8 encoding and may have at most 50,000 addresses and 50 MB (uncompressed). A larger website splits the sitemap into several files and puts their list into a sitemap index:
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://www.example.com/sitemap-articles.xml</loc>
<lastmod>2026-10-09</lastmod>
</sitemap>
<sitemap>
<loc>https://www.example.com/sitemap-products.xml</loc>
</sitemap>
</sitemapindex>
Put the sitemap in the root of the website. A sitemap in the /articles/ folder applies only to addresses from that folder (unless you submit it through Search Console) and a sitemap given in robots.txt only to addresses from the same domain.
A sitemap from a database
On a dynamic website, the sitemap is generated by a script, so it is always up to date. The XMLWriter class (part of PHP) itself converts characters in the addresses that have to be written differently in XML. The example reads the articles table with the columns url (the path without the domain), modified (the time of the last significant change as a timestamp) and visible:
<?php
// sitemap.php – sitemap from the database; in .htaccess: RewriteRule ^sitemap\.xml$ sitemap.php [L]
$pdo = require __DIR__.'/db.php';
date_default_timezone_set('Europe/Prague');
header('Content-Type: application/xml; charset=utf-8');
$x = new XMLWriter();
$x->openURI('php://output');
$x->setIndent(true);
$x->setIndentString(' ');
$x->startDocument('1.0', 'UTF-8');
$x->startElement('urlset');
$x->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
foreach($pdo->query('SELECT url, modified FROM articles WHERE visible = 1 ORDER BY url') as $row)
{
$x->startElement('url');
$x->writeElement('loc', 'https://www.example.com/'.$row['url']);
$x->writeElement('lastmod', date('c', $row['modified']));
$x->endElement();
}
$x->endElement();
$x->endDocument();
$x->flush();
For three published articles (one has parameters in its address) and one hidden article, it prints:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/about-us/</loc>
<lastmod>2026-10-01T09:00:00+02:00</lastmod>
</url>
<url>
<loc>https://www.example.com/events/?id=5&type=concert</loc>
<lastmod>2026-06-20T18:00:00+02:00</lastmod>
</url>
<url>
<loc>https://www.example.com/menu/</loc>
<lastmod>2026-09-15T10:30:00+02:00</lastmod>
</url>
</urlset>
The date() function with the c format writes the time according to the ISO 8601 standard including the time zone offset, as the sitemap requires. With tens of thousands of articles, split the script according to the format above: a sitemap index and individual sitemaps of at most 50,000 addresses each (LIMIT and OFFSET in the query).
Checking against the schema
The sitemap format is described by an XSD schema on the sitemaps.org website (sitemap.xsd for a sitemap, siteindex.xsd for a sitemap index). PHP checks the sitemap against it with the DOMDocument class:
<?php // check-sitemap.php – sitemap.xsd downloaded from https://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->load(__DIR__.'/sitemap.xml'); if($dom->schemaValidate(__DIR__.'/sitemap.xsd')) echo "The sitemap matches the schema.\n"; else foreach(libxml_get_errors() as $error) echo 'Line '.$error->line.': '.trim($error->message)."\n";
A sitemap with a date written in a human-readable form (<lastmod>1 Oct 2026</lastmod>) does not pass:
Line 5: Element '{http://www.sitemaps.org/schemas/sitemap/0.9}lastmod': '1 Oct 2026' is not a valid value of the union type '{http://www.sitemaps.org/schemas/sitemap/0.9}tLastmod'.
The schema checks the format, not the content. Whether the addresses in the sitemap return 200 and are not redirected is shown only by the sitemaps report in Search Console or by your own script that goes through the addresses.
How to hand the sitemap over to search engines
- The line Sitemap: https://www.example.com/sitemap.xml in robots.txt – all search engines find it, see robots.txt, noindex and X-Robots-Tag.
- Submission in Google Search Console (Sitemaps), Bing Webmaster Tools and Seznam Webmaster. In Search Console you then see when Google downloaded the sitemap, how many addresses it found in it and which errors.
The former “ping” – announcing a new sitemap by calling an address of the search engine – has been discontinued by both Google and Bing. It is enough to keep the sitemap up to date and the search engine downloads it by itself.
IndexNow: announcing changed addresses
The IndexNow protocol lets you tell a search engine immediately that a page has changed, been added or disappeared. It is used by Bing, Seznam, Yandex, Naver and others, and what you send to one of them, the others get too. Google does not use IndexNow (as of October 2026). The procedure:
- Make up a key: 8 to 128 characters from unaccented letters, digits and the hyphen.
- Save it into a text file key.txt in the root of the website – the content of the file is the key itself. The search engine uses the file to verify that the addresses are sent by the administrator of the website.
- After a page changes, send the addresses to https://api.indexnow.org/indexnow (or directly to a search engine, for example https://www.bing.com/indexnow). Up to 10,000 of them can be sent at once.
<?php
// indexnow.php – announces changed addresses; the file https://www.example.com/5f2a8c1e9b7d4e3f.txt contains the key
$data = array(
'host' => 'www.example.com',
'key' => '5f2a8c1e9b7d4e3f',
'urlList' => array('https://www.example.com/menu/', 'https://www.example.com/events/autumn-tasting/'),
);
$ch = curl_init('https://api.indexnow.org/indexnow');
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array('Content-Type: application/json; charset=utf-8'),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
));
curl_exec($ch);
echo 'IndexNow: HTTP '.curl_getinfo($ch, CURLINFO_RESPONSE_CODE)."\n";
IndexNow: HTTP 202
The response 200 means accepted, 202 accepted with the key verified later, 403 the key was not found or is wrong, 422 addresses from a domain other than host and 429 too many requests. Not even 200 guarantees that the search engine indexes the page. Call the script when an article is saved in the administration, not on every page view.
A multilingual website: hreflang
When a page has versions in several languages (or for different countries in the same language), tell the search engine with hreflang links. In the results it then shows the version according to the language of the searcher and does not consider the language versions duplicates. The head of each version gets the complete set of links, including the link to itself:
<link rel="alternate" hreflang="en" href="https://www.example.com/menu/"> <link rel="alternate" hreflang="cs" href="https://www.example.com/cs/menu/"> <link rel="alternate" hreflang="de" href="https://www.example.com/de/speisekarte/"> <link rel="alternate" hreflang="x-default" href="https://www.example.com/menu/">
- The language code according to ISO 639-1 (en, cs, sk, de), optionally with a country code according to ISO 3166-1 (en-GB, de-AT). Czech is cs – cz is a country code, not a language code, and a country code alone is not a valid value. Unofficial country codes such as en-UK are invalid too (the United Kingdom is GB).
- x-default – the page for visitors whose language is not among the versions (often the English version or a language selector).
- The links must be mutual. When the English version links to the Czech one but the Czech one does not link back, Google ignores the links. The most common mistake of multilingual websites.
- Full addresses with the protocol. The versions may also be on different domains (example.com, example.de).
- Each version has a canonical address pointing to itself, not to the version in the main language – otherwise the other languages would drop out of the index.
Google recognizes the language of a page from its content; it does not use the lang attribute of the html element or hreflang for that. The lang attribute belongs on every page anyway, for screen readers and browsers (hyphenation, translation).
In PHP, the set is printed by a loop over the list of versions, the same for all language versions of the page:
<?php // language versions of the page (from the database or the settings of the site); each version prints the same set including itself $versions = array( 'en' => 'https://www.example.com/menu/', 'cs' => 'https://www.example.com/cs/menu/', 'de' => 'https://www.example.com/de/speisekarte/', ); $versions['x-default'] = $versions['en']; foreach($versions as $language => $address) echo '<link rel="alternate" hreflang="'.$language.'" href="'.htmlspecialchars($address).'">'."\n";
Instead of tags in the head, the same set can be written into the HTTP Link header (for PDFs) or into the sitemap – each <url> element then contains <xhtml:link> links to all versions including itself:
<?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">
<url>
<loc>https://www.example.com/menu/</loc>
<xhtml:link rel="alternate" hreflang="en" href="https://www.example.com/menu/"/>
<xhtml:link rel="alternate" hreflang="cs" href="https://www.example.com/cs/menu/"/>
</url>
<url>
<loc>https://www.example.com/cs/menu/</loc>
<xhtml:link rel="alternate" hreflang="en" href="https://www.example.com/menu/"/>
<xhtml:link rel="alternate" hreflang="cs" href="https://www.example.com/cs/menu/"/>
</url>
</urlset>
All three ways have the same weight for Google, one is enough. The sitemap is suitable for large websites, where the sets in the head of every page would be long. Such a sitemap does not pass the check against sitemap.xsd described above, though: the schema allows elements from other namespaces only when it knows their schema too, and reports the error No matching global element declaration available, but demanded by the strict wildcard. Google accepts the sitemap – check it in Search Console, or against the schema after removing the <xhtml:link> elements.
Do not redirect by the browser language
An automatic redirect to a language version based on the Accept-Language header or the country of the IP address looks convenient, but Google mostly crawls from the USA and usually without the Accept-Language header – it might not see the other language versions at all. In addition, the visitor cannot choose a different language than the one the website forces on them. It is better to offer the language version with a bar or a link and leave the choice to the visitor.
Related articles:
mod_rewrite and clean URLs
Canonical URLs and duplicate content
Structured data and link previews
What a REST API is and how to return JSON in PHP