Loading.......
Loading...

Make Dynamic XML sitemap using PHP

Posted on 12th November, 2025
PHP DYNAMIC SITEMAP HOW TO CREATE A SITEMAP IN PHP WEBSITE HOW TO CREATE DYNAMIC SITEMAP IN PHP HOW TO CREATE SITEMAP FOR DYNAMIC WEBSITE PHP PHP SITEMAP SITEMAP IN PHP SITEMAP.PHP SCRIPT
This tutorial teaches you how to create a XML Sitemap for your dynamic website using PHP.

What is a sitemap?

A sitemap is a file that lists the pages on a website to help search engines like Google and users navigate and understand the site's structure. It provides valuable information, such as when pages were last updated, and allows you to prioritize important content like videos or images.

Types of sitemaps

XML sitemaps: These are specifically for search engines to read and are formatted for bots to efficiently crawl and index your site.
HTML sitemaps: These are designed for users and can be found directly on a website, typically serving as a navigational aid or directory.
Visual sitemaps: These are often used by designers and planners to create a blueprint of a website's structure, showing the relationship between pages.

To create a XML Sitemap for your website, you need to first of all store the data that we are going to display in the sitemap such as URL of your dynamic website pages or contents and their dates in a table in your database for easy retrieval into the sitemap when needed.

Lets get started

We are first going to create a test database table where we will store the data that we will display in the sitemap. Just copy the query below and run them in your database to create the needed test table.

DATABASE TEST TABLE NAMED AS pages

CREATE TABLE `pages` (
  `page_id` int(11) NOT NULL,
  `page_title` text NOT NULL,
  `page_url` text NOT NULL,
  `date` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;


ALTER TABLE `pages`
  ADD PRIMARY KEY (`page_id`);


ALTER TABLE `pages`
  MODIFY `page_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;


INSERT INTO `pages` (`page_id`, `page_title`, `page_url`, `date`) VALUES
(1, 'Comment System using PHP, Ajax and MySQLi', '7/comment-system-using-php-ajax-and-mysqli', '2025-10-23'),
(2, 'Chat Script Similar to Facebook using Ajax and PHP', '32/chat-script-similar-to-facebook-using-ajax-and-php', '2025-10-23'),
(3, 'Create Treeview with Bootstrap Treeview Ajax JQuery in PHP\r\n', 'create-treeview-with-bootstrap-treeview-ajax-jquery-in-php', '2025-10-23'),
(4, 'Bootstrap Multiselect Dropdown with Checkboxes using Jquery in PHP\r\n', 'bootstrap-multiselect-dropdown-with-checkboxes-using-jquery-in-php', '2025-10-23'),
(5, 'Facebook Style Popup Notification using PHP Ajax Bootstrap\r\n', 'facebook-style-popup-notification-using-php-ajax-bootstrap', '2025-10-23'),
(6, 'Modal with Dynamic Previous & Next Data Button by Ajax PHP\r\n', 'modal-with-dynamic-previous-next-data-button-by-ajax-php', '2025-10-23'),
(7, 'How to Use Bootstrap Select Plugin with Ajax Jquery PHP\r\n', 'how-to-use-bootstrap-select-plugin-with-ajax-jquery-php', '2025-10-23'),
(8, 'How to Load CSV File data into HTML Table Using AJAX jQuery\r\n', 'how-to-load-csv-file-data-into-html-table-using-ajax-jquery', '2025-10-23'),
(9, 'Autocomplete Textbox using Typeahead with Ajax PHP Bootstrap\r\n', 'autocomplete-textbox-using-typeahead-with-ajax-php-bootstrap', '2025-10-23'),
(10, 'Export Data to Excel in Codeigniter using PHPExcel\r\n', 'export-data-to-excel-in-codeigniter-using-phpexcel', '2025-10-23');

Create a dot php file and name it as sitemap.php, copy and paste the following code in this file, save the file and view it via your web browser to see your sitemap in action.

Code for sitemap.php

<?php 
// Database Connection Details
$connect = mysqli_connect("localhost", "root", "", "vasplus"); // Input your hostname, username, password and database name

$data = mysqli_query($connect, "select page_url,date from pages"); // Query the pages table to get all URL and dates posted

if( mysqli_num_rows($data) > 0 ) // Check if we have some data in the database pages table
{
	$base_url = "http://localhost/tutorial/";
	
	header("Content-Type: application/xml; charset=utf-8");
	
	echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; 
	
	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">' . PHP_EOL;
	
	// Fetch all needed data and display in the sitemap
	while($row = mysqli_fetch_array($data))
	{
		ob_start();
		echo '<url>'.PHP_EOL;
		echo '<loc>'.$base_url. $row["page_url"] .'</loc>'.PHP_EOL;
		echo '<lastmod>'.$row["date"].'</lastmod>'.PHP_EOL;
		echo '<changefreq>daily</changefreq>'.PHP_EOL;
		echo '</url>'.PHP_EOL;
		ob_end_flush();
	}
	
	echo '</urlset>'.PHP_EOL;
}
else
{
	// There is no data to display in our sitemap at the moment.
}

?>

Post Comment
Press Enter to send
No comment yet.

Submit your Job or Project Today!

We can help you turn your idea into reality, take over your existing project, or extend your current development team.

Submit your idea job or project below and we will follow up with you shortly.

OUR OBJECTIVE

Our objective is to reach a place where our services will be highly regarded by businesses from various industrial domains for building their innovative busines solutions with our cutting-edge technological expertise, interactive designs and uncompromised quality.

OUR MISSION

We aspire to help businesses ranging from startups to enterprises, who reach out to us with their requirements, in achieving great lengths, expanding their reach, upscaling their products, and generate a large user-base with our outstanding and cost-effective services.

Please Wait....
Please Wait...