Creating A Sitemap For Search Engines

Creation of a sitemap XML file is easy using PHP

Structure Of The Sitemap

XML sitemaps can be submitted to all of the major search engine providers to identify the files within a website that you want to be indexed. The structure of a sitemap is defined at and consists of a set of header information followed by a repeating set of information for each file to be indexed.

The only mandatory item of information is the URL of the file. All other information eg <changefreq> is optional. All of these elements are defined in detail at


<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="">
 . repeated for each URL

Creating The Sitemap

Creation of the sitemap uses DOMImplementation functions to create the structure, given an array of URLs to be inserted. The resulting structure is formatted to look 'pretty' with formatOutput = true and saved to a nominated .xml file withsave($outputFile).

The basic sequence of creating the structure is that an element is created within the DOM eg $urlData = $dom->createElement('url') and then appended as a child to a previously defined element eg $urlSet->appendChild($urlData). The previous example creates an element with no contained data. If the element has contained data, this is specified when it is created eg $dom->createElement('changefreq','daily')


public function createSitemap(
 $xmlSiteMap = new DOMImplementation();
 /* create the basic document */
 $dom = $xmlSiteMap->createDocument();
 /* set the encoding */
 $dom->encoding = "UTF-8";
 /* define the namespace */
 $urlSet = $dom->createElementNS(
 foreach ($urlList as $url) {
   $urlData = $dom->createElement('url');
   /* the url is the minimum info required */
   $locData = $dom->createElement(
   /* add any optional elements */
 /* make it look pretty */
 $dom->formatOutput = true;
 $saveResult = $dom->save($outputFile);
 if ($saveResult === false) {
   return false;
 } else {
   return true;


Validating The Sitemap

It is useful to validate the structure of the sitemap before submitting it to a search engine. This is done using the schemaValidate function, supplied with a XML Schema Definition file (.xsd) provided at Normally any errors found would be displayed on the screen, but if necessary they can be trapped and handled in whatever way appropriate.


public function validateSitemap($inputFile)
  /* stop validation errors going to the display */
  $dom = new DOMDocument();
  /* load the sitemap file */
  $validateResult = $dom->schemaValidate(
  if (!$validateResult) {
    /* get the errors into an array */
    $errors = libxml_get_errors();
    $return = $errors;
  } else {
    $return = true;
  /* direct errors back to the display */
  return $return;

Submitting The Sitemap

A sitemap can be manually submitted to a search engine website, but the easiest way is to provide an entry in the robots.txt file.


User-agent: *
Disallow: /test/