This script is designed to be a standalone tool that you run whenever you update your site content. It transforms your raw XML data into a high-performance, SEO-friendly static directory.
The Complete Script: generate_sitemap.php
<?php
/**
* NathanLegakis.com Sitemap Generator
* Creates a static /sitemap/index.html from sitemap.xml
*/
// 1. Configuration
$xmlSource = ‘sitemap.xml’;
$targetDir = ‘sitemap’;
$outputFile = $targetDir . ‘/index.html’;
$message = “”;
// 2. Generation Logic
if (isset($_POST[‘generate’])) {
// Create directory if missing
if (!is_dir($targetDir)) {
mkdir($targetDir, 0755, true);
}
$xml = @simplexml_load_file($xmlSource);
if ($xml) {
$timestamp = date(“F j, Y, g:i a”);
ob_start(); // Start capturing HTML output
?>
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>Sitemap | Nathan Legakis</title>
<style>
:root { –bg: #0a0a0a; –card: #111; –text: #ededed; –dim: #a0a0a0; –blue: #3b82f6; –border: #262626; }
body { background: var(–bg); color: var(–text); font-family: -apple-system, BlinkMacSystemFont, “Segoe UI”, Roboto, sans-serif; padding: 60px 20px; line-height: 1.6; margin: 0; }
.container { max-width: 850px; margin: 0 auto; }
header { margin-bottom: 3rem; border-left: 4px solid var(–blue); padding-left: 20px; }
h1 { font-size: 2.5rem; margin: 0; letter-spacing: -1px; }
.update-tag { font-size: 0.85rem; color: var(–dim); text-transform: uppercase; margin-top: 5px; display: block; }
.grid { border: 1px solid var(–border); background: var(–card); border-radius: 12px; overflow: hidden; }
ul { list-style: none; padding: 0; margin: 0; }
li { padding: 20px; border-bottom: 1px solid var(–border); transition: background 0.2s; }
li:hover { background: #161616; }
li:last-child { border-bottom: none; }
a { color: var(–text); text-decoration: none; font-weight: 600; font-size: 1.15rem; display: block; }
a:hover { color: var(–blue); }
.meta { display: block; font-size: 0.85rem; color: var(–dim); margin-top: 5px; font-family: monospace; }
footer { margin-top: 4rem; text-align: center; font-size: 0.85rem; color: var(–dim); border-top: 1px solid var(–border); padding-top: 2rem; }
</style>
</head>
<body>
<div class=”container”>
<header>
<h1>Sitemap</h1>
<span class=”update-tag”>Generated: <?= $timestamp ?></span>
</header>
<div class=”grid”>
<ul>
<?php foreach ($xml->url as $url):
$loc = (string)$url->loc;
$path = parse_url($loc, PHP_URL_PATH);
$label = basename($path);
$label = str_replace([‘-‘, ‘.html’, ‘.php’], [‘ ‘, ”, ”], $label);
$label = empty($label) ? ‘Home’ : ucwords($label);
?>
<li>
<a href=”<?= $loc ?>”><?= $label ?></a>
<span class=”meta”><?= $loc ?></span>
</li>
<?php endforeach; ?>
</ul>
</div>
<footer>
<p>© <?= date(“Y”) ?> Nathan Legakis • <a href=”/” style=”color: var(–dim); font-size: 0.85rem; font-weight: 400;”>Back to Home</a></p>
</footer>
</div>
</body>
</html>
<?php
$htmlContent = ob_get_clean();
// Write content to sitemap/index.html
if (file_put_contents($outputFile, $htmlContent)) {
$message = “<div class=’success’>Success! Static sitemap created.<br><a href=’/$targetDir/’>View Sitemap</a></div>”;
} else {
$message = “<div class=’error’>Error: Unable to write file. Check permissions.</div>”;
}
} else {
$message = “<div class=’error’>Error: Could not find sitemap.xml in root.</div>”;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sitemap Builder</title>
<style>
body { background: #000; color: #fff; font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
.panel { background: #111; padding: 40px; border-radius: 12px; border: 1px solid #222; text-align: center; width: 350px; box-shadow: 0 10px 30px rgba(0,0,0,0.5); }
h2 { margin-top: 0; font-size: 0.9rem; color: #666; text-transform: uppercase; letter-spacing: 2px; margin-bottom: 20px; }
button { background: #3b82f6; color: white; border: none; padding: 14px; border-radius: 8px; cursor: pointer; font-weight: bold; width: 100%; font-size: 1rem; transition: 0.2s; }
button:hover { background: #2563eb; transform: translateY(-1px); }
.success { margin-top: 25px; color: #4ade80; line-height: 1.5; }
.success a { color: #fff; font-weight: bold; }
.error { margin-top: 25px; color: #f87171; }
</style>
</head>
<body>
<div class=”panel”>
<h2>Sitemap Tool</h2>
<form method=”post”>
<button type=”submit” name=”generate”>Build Static HTML</button>
</form>
<?= $message ?>
</div>
</body>
</html>
Summary of How It Works
* Parsing: The script uses simplexml_load_file() to ingest your existing sitemap.xml. It strips out file extensions and dashes to create clean, readable page titles automatically.
* Static Generation: By using Output Buffering (ob_start), it captures the fully rendered HTML and saves it as a physical index.html file. This means the server doesn’t have to “think” when a user visits the sitemap; it just serves the static text.
* SEO Structure: The code automatically creates a /sitemap/ directory. This results in a clean URL (nathanlegakis.com/sitemap/), which Google treats with higher authority than a floating file like sitemap.php.
* Visual Consistency: The CSS is tailored to your site’s dark-mode brand, using a deep background, subtle borders, and the signature blue accent on hover.
Discover more from NathanLegakis.com
Subscribe to get the latest posts sent to your email.
Leave a Reply