Generating a sitemap for a static site

How to move beyond a collection of HTML files

Photo by Nik Shuliahin on Unsplash

The aim

The setup


GitHub pages

XML-compatible HTML

Canonical URLs

The method


$files = find_files(".", "html");
$doc = new DOMDocument();
$res = @$doc->load($file);
$xpath = new DOMXPath($doc);
$nodes = $xpath->query("/html/head/link[@rel='canonical']/@href");
$loc = trim($nodes->item(0)->value);
$mtime = filemtime($file);

The result

/var/www/mysite $ php ~/bin/generate.php . > sitemap.xml

Final words

Technologist & writer, Bobby is currently working on several projects including a management dashboard for static websites and an education portal.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store