Generate a Sitemap XML in PHP

A sitemap.xml is a file that contains the important URLs of a website to help search engines understand the URLs on the website in an efficient manner. Learn how to generate an XML sitemap in PHP in this post.

Generate Sitemap XML in PHP

Every website uses a file sitemap.xml, which is an XML (Extensible Markup Language) file containing information about website URLs, making the crawling job easier for search engines. A sitemap.xml makes it easier for search engines to crawl a website and index pages found in the sitemap file. It plays a key role in improving a website's SEO performance. In this post, we will learn how to create a sitemap XML file dynamically in a PHP application.

 

What is The Basic Structure of an XML File?

An XML file follows a basic document structure to define URLs found on a website containing the file. Each entry in the sitemap file must contain a URL of the page and the date when the page was last updated or modified. The tags used in the <url> element of a sitemap XML file are as below:

  • <loc>: The URL of a specific page.
  • <lastmod>: The date when that specific page was last modified.
  • <changefreq>: The string defining how often a specific page is modified or changed.
  • <priority>: The priority of a specific page is defined by values between 0.0 and 1.0.

Not all tags are required, but <loc> and <lastmod> are highly recommended for better indexing. 

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.your-website.com/</loc>
<lastmod>2021-04-20T22:43:11Z</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://www.your-website.com/about</loc>
<lastmod>2021-04-20T22:43:11Z</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
</urlset>
 

How to Generate a Sitemap XML File in PHP?

Creating a sitemap XML file in PHP is very easy using the DOM Document object. We will create a sitemap.xml file by opening a DOM document, adding required elements to the document, and finally saving the content as an XML file. This script can be used as an XML sitemap generator to automatically generate a sitemap for a website. Steps to generate a sitemap file are as follows:

  • Define a constant BASE_URL for the website's main domain address.
  • Define an array of URLs containing a slug and a modified date.
  • Open a new DOM document in PHP and set the output format for indentation.
  • Create a urlset element and set the xmlns attribute to the element.
  • Append the urlset element to the document.
  • Loop through each array of URLs and create a URL element.
  • Create a loc element with the complete URL as the content of the element.
  • Append the loc element to the url element.
  • Create a lastmod element with last modified date as the content of the element.
  • Append the lastmod element to the url element.
  • Finally, append the url element to the urlset element.
  • As the last step, save the document as XML file with the sitemap.xml name.
<?php
define('BASE_URL', 'https://'.$_SERVER['SERVER_NAME']);

$website_urls = [
['slug' => 'about-us', 'updated_at' => '2021-04-02 08:44:38'],
['slug' => 'services', 'updated_at' => '2021-04-03 05:13:25'],
['slug' => 'our-team', 'updated_at' => '2021-04-04 07:34:08'],
];

// Create a new DOMDocument object
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true; // Make the XML output pretty (with indentation)

// Create the <urlset> element (the root element)
$url_set = $dom->createElement('urlset');
$url_set->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

// Append the <urlset> element to the document
$dom->appendChild($url_set);

// Loop through each URL and create the <url> XML tags
foreach ($website_urls as $url) {
// Create the <url> element
$url_element = $dom->createElement('url');

// Create <loc> (URL location) element
$loc = $dom->createElement('loc', BASE_URL .'/'. $url['slug']);
$url_element->appendChild($loc);

// Create <lastmod> (Last Modified) element
$last_mod = $dom->createElement('lastmod', date('Y-m-d\TH:i:s\Z', strtotime($url['updated_at'])));
$url_element->appendChild($last_mod);

// Append the <url> element to the <urlset> parent element
$url_set->appendChild($url_element);
}

// Save the XML to a file (sitemap.xml)
$dom->save('sitemap.xml');

The code snippet uses a static array of URLs for the sitemap, but this data can come from a database table. The table should contain all the necessary data for the URL needed in the sitemap.xml file. This script can be used to create a sitemap XML file as a command or via an AJAX request. The result XML sitemap will be like this:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.your-website.com/about-us</loc>
<lastmod>2021-04-02T08:44:38Z</lastmod>
</url>
<url>
<loc>https://www.your-website.com/services</loc>
<lastmod>2021-04-03T05:13:25Z</lastmod>
</url>
<url>
<loc>https://www.your-website.com/our-team</loc>
<lastmod>2021-04-04T07:34:08Z</lastmod>
</url>
</urlset>

Generating an XML sitemap in PHP is a simple yet powerful way to improve a website’s SEO. With the code snippet in this article, we can generate a dynamic and updated sitemap.xml file. An updated sitemap.xml not only helps search engines to better understand and index content but also helps to achieve better online visibility.