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

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 sitemap file. It plays a key role in improving website's SEO performance. In this post we will learn how to create sitemap XML file in PHP dynamically.
What is Basic Structure of an XML File?
An XML file follows a basic document structure to define URLs found a website containing the file. Each entry in sitemap file must contain a URL of page and date when the page was last updated or modified. The tags used in <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 specific page defined by values between 0.0 to 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 and add required elements to document and finally save the content as an XML file. This script can be used as XML sitemap generator to automatically generate sitemap for a website. Steps to generate sitemap file are as below:
- Define a constant
BASE_URLfor website's main domain address. - Define an array of URLs containing a slug and modified date.
- Open a new DOM document in PHP and set the output format for indentation.
- Create a
urlsetelement and setxmlnsattribute to element. - Append the
urlsetelement to document. - Loop through each array of URLs and create a URL element.
- Create
locelement with complete URL as content of element. - Append
locelement to url element. - Create
lastmodelement with last modified date as content of element. - Append
lastmodelement to url element. - Finally, append
urlelement to urlset element. - As last step save the document as XML file with
sitemap.xmlname.
<?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 sitemap, but this data can come from database table. The table should contain all the necessary data for URL needed in sitemap.xml file. This script can be used to create sitemap XML file as command or via 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 code snippet in this article we can generate 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.