Create a gist now

Instantly share code, notes, and snippets.

Generate XML sitemap from list of urls
* This is a quick way to turn a simple text file
* with a list of urls in a text file (sitemap-urls.txt)
* into a valid XML Sitemap:
* Put this file sitemap.xml.php and sitemap-urls.txt at
* the webroot
* Then add the text in quotes below to your robots.txt file as a new line:
* "Sitemap:"
* Questions? email
$filename = 'sitemap-urls.txt';
$urls = file($filename);
$filectime = filectime($filename);
$urls = array_map('trim',$urls);
$sitemap = array();
foreach($urls as $url) {
if ($url != '') {
$priority = '0.5';
$sitemap[] = array(
'loc' => $url,
'lastmod' => date('Y-m-d',$filectime),
'changefreq' => 'weekly',
'priority' => $priority,
header('Content-Type: text/xml');
echo '<?xml version=\'1.0\' encoding=\'UTF-8\'?>';
echo "\n";
echo '<urlset xmlns=""
echo "\n";
foreach ($sitemap as $link) {
echo "\t<url>\n";
echo "\t\t<loc>" . htmlentities($link['loc']) . "</loc>\n";
echo "\t\t<lastmod>{$link['lastmod']}</lastmod>\n";
echo "\t\t<changefreq>{$link['changefreq']}</changefreq>\n";
echo "\t\t<priority>{$link['priority']}</priority>\n";
echo "\t</url>\n";
echo '</urlset>';
Zeokat commented Mar 9, 2014

Good one, i need something like this to generate sitemap of some static resources at Vozidea. Thanks for the code!

artlung commented Jun 13, 2016

If you have a need to break up a text file with more than 10,000 urls, use this:

Shoora commented Jun 22, 2016

artlung, thx man!!

sompil commented Jan 27, 2017

thanks artlung!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment