Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate XML sitemap from list of urls
<?php
/**
*
* 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:
* http://en.wikipedia.org/wiki/Sitemaps
* Put this file sitemap.xml.php and sitemap-urls.txt at
* the webroot http://example.com/sitemap.xml.php
* Then add the text in quotes below to your robots.txt file as a new line:
* "Sitemap: http://example.com/sitemap.xml.php"
* Questions? email joe@artlung.com
*/
$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="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
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

This comment has been minimized.

Copy link

@Zeokat 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

This comment has been minimized.

Copy link
Owner Author

@artlung artlung commented Jun 13, 2016

If you have a need to break up a text file with more than 10,000 urls, use this: https://gist.github.com/artlung/613e6ac577a170bf2b4b9045486f129e

@Shoora

This comment has been minimized.

Copy link

@Shoora Shoora commented Jun 22, 2016

artlung, thx man!!

@dianariyanto

This comment has been minimized.

Copy link

@dianariyanto dianariyanto commented Jan 27, 2017

thanks artlung!

@PriyamBG

This comment has been minimized.

Copy link

@PriyamBG PriyamBG commented Nov 9, 2017

I need your Help. how to i implement this code if any tutorial kindly share here my site is https://www.braingroom.com/

@PrestaShark

This comment has been minimized.

Copy link

@PrestaShark PrestaShark commented Dec 18, 2017

Very nice! <3

@shaniax420

This comment has been minimized.

Copy link

@shaniax420 shaniax420 commented Aug 4, 2019

super script <3

@rk-world

This comment has been minimized.

Copy link

@rk-world rk-world commented Aug 24, 2019

I have a list of URLs in 10 columns of a CSV file and I need a separate sitemap for each column. Can we modify this code accordingly?

@artlung

This comment has been minimized.

Copy link
Owner Author

@artlung artlung commented Aug 24, 2019

I have a list of URLs in 10 columns of a CSV file and I need a separate sitemap for each column. Can we modify this code accordingly?

Check out https://gist.github.com/artlung/311325ac4531c7a2cfa72ee4d4eb7bb5 @rk-world

@riyas-rawther

This comment has been minimized.

Copy link

@riyas-rawther riyas-rawther commented Sep 12, 2020

Thanks very much for the script. I have created the jquery version of this on my website.
Generate sitemap from list of URLs

@app19191

This comment has been minimized.

Copy link

@app19191 app19191 commented Oct 10, 2020

Thank you, exactly what I was looking for. However, it looks like the doesn't update by itself daily. In order to have it updated, I need to save the sitemap-urls.txt file. There is a way hoe I can have this done automatically everyday?

@artlung

This comment has been minimized.

Copy link
Owner Author

@artlung artlung commented Oct 11, 2020

Thank you, exactly what I was looking for. However, it looks like the doesn't update by itself daily. In order to have it updated, I need to save the sitemap-urls.txt file. There is a way hoe I can have this done automatically everyday?

There are a number of different ways to automate a task, typically on a Unix based system you would use cron. But it would depend on your expectations how to use that. You’d also need to automate the URL list as well in some manner.

@riyas-rawther

This comment has been minimized.

Copy link

@riyas-rawther riyas-rawther commented Oct 12, 2020

Thank you, exactly what I was looking for. However, it looks like the doesn't update by itself daily. In order to have it updated, I need to save the sitemap-urls.txt file. There is a way hoe I can have this done automatically everyday?

You need a cron job + the source of the URLs (database/json or whatever) and a mofified php script like this...

@app19191

This comment has been minimized.

Copy link

@app19191 app19191 commented Oct 13, 2020

Thank you, exactly what I was looking for. However, it looks like the doesn't update by itself daily. In order to have it updated, I need to save the sitemap-urls.txt file. There is a way hoe I can have this done automatically everyday?

You need a cron job + the source of the URLs (database/json or whatever) and a mofified php script like this...

Hi,

Thank you for your reply.

Do you have a working solution for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.