PHP/XML Sitemap Generator
Page last updated on 2011 / 04 / 09XML Sitemaps are a useful method for search engines to quickly pick up new content they would otherwise have to find via hyperlinks on the web. Sitemaps can also provide useful metadata regarding URLs such as its last modification date, how often the content changes and the relative importance of the URL in comparison to the rest of the site.
The following code allows you to easily maintain an XML sitemap using PHP. You can:
- Generate a sitemap from scratch
- Add URLs to the sitemap
- Edit URL's metadata in the sitemap
- Delete URLs from the sitemap
The script is very simple and currently only takes into account a URL and its last modification date, with the latter being optional. This allows you to easily populate the sitemap with existing and new URLs and easily indicate when content has been updated.
If you have a large number of URLs, you will need to tinker the script to accommodate more than one sitemap, and also have a sitemap index file.
Simply save the contents of the two scripts below and try it for yourself. The references to Sitemap.xml assume that you are in your domain's root directory, otherwise, you should ensure your Sitemap.xml is saved in the root directory to avoid complications, unless it is referenced in a sitemap index file.
<?php // Save as sitemap.php class sitemap { public function load() { global $dom; $dom = new DOMDocument; $dom->preserveWhiteSpace = true; $dom->loadXML(file_get_contents('Sitemap.xml')); } public function generate() { global $dom,$urlset; $dom = new DOMDocument(); $dom->loadXML('<?xml version="1.0" encoding="UTF-8"?> <urlset xml:ns="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"> </urlset>'); $dom->save('Sitemap.xml'); } public function addrow($vars) { global $dom,$urlset; if(!isset($urlset)) $urlset = $dom->getElementsByTagName('urlset')->item(0); $node = $dom->createElement('url'); $node->setAttribute('id',md5($vars['loc'])); foreach($vars as $key => $var) { $node2 = $dom->createElement($key); $node3 = $dom->createTextNode($var); $node2->appendChild($node3); $node->appendChild($node2); } $newnode = $urlset->appendChild($node); $vars['md5'] = md5($vars['loc']); } public function editrow($id) { global $dom; $xpath = new DOMXPath($dom); $mod = $xpath->query("/urlset/url[@id='$id']/lastmod"); $mod->item(0)->nodeValue = strftime("%Y-%m-%d",time() - gmmktime() + mktime()); $dom->save('Sitemap.xml'); } public function deleterow($id) { global $dom; $xpath = new DOMXPath($dom); $urlset = $dom->getElementsByTagName('urlset')->item(0); $row = $xpath->query("/urlset/url[@id='$id']")->item(0); if($row) $row->parentNode->removeChild($row); $dom->save('Sitemap.xml'); } } ?> <?php // Save as test.php // Example URLs $urls = array( 'http://www.example.com/', 'http://www.example.com/page2', 'http://www.example.com/page3', 'http://www.example.com/page4', 'http://www.example.com/page5', 'http://www.example.com/page6' ); include_once('sitemap.php'); // Action 1. Create a sitemap $_sitemap = new sitemap; $_sitemap->generate(); // Action 2. Add URLs to the sitemap (e.g. when you add new content) // Using a fictional "last modified" date for each URL of 2010-01-01, otherwise // Use strftime("%Y-%m-%d",$unix_timestamp) for actual last modification dates of URLs or Use strftime("%Y-%m-%d",time()) to set the last modification date for today // (e.g. a file's last modified date or a mySQL column) $_sitemap = new sitemap; $_sitemap->load(); // Load up Sitemap.xml foreach($urls as $url) // Iterate through example URLs and add to sitemap { $array = array('loc'=>$url,'lastmod'=>'2010-01-01'); $_sitemap->addrow($array); } $dom->save('Sitemap.xml'); // Save sitemap // Action 3. Update a last modification date (e.g. when you update existing content) // Each URL in the sitemap has an MD5 attribute so uniquely reference it $_sitemap = new sitemap; $_sitemap->load(); $_sitemap->editrow(md5($urls[1])); // Action 4. Delete a URL from the sitemap (e.g. when a URL is no longer available on your site) $_sitemap = new sitemap; $_sitemap->load(); $_sitemap->deleterow(md5($urls[2])); ?>
For further reading regarding sitemaps, visit Sitemaps.org.
Tweet