How do I use PHP to generate dynamic sitemaps?

Generating Dynamic Sitemaps with PHP

A sitemap is an XML file that lists the URLs of a website, making it easier for search engines to crawl and index your site’s content. A dynamic sitemap is one that is automatically updated as new content is added to the site. In this blog post, we will discuss how to generate a dynamic sitemap using PHP. This can be especially useful for websites with frequently changing content, such as blogs, e-commerce stores, or news sites.

Step 1: Create a PHP script to generate the sitemap

First, we will create a PHP script that generates an XML sitemap from the URLs of your website. This script will fetch the URLs from your database and create an XML sitemap that conforms to the sitemap protocol.

Create a new file called “generate_sitemap.php” and add the following code:

header('Content-Type: application/xml; charset=utf-8');

// Replace the following with your own database connection details
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

// Fetch URLs from the database
$sql = "SELECT url, last_modified FROM urls";
$result = $conn->query($sql);

// Generate the XML sitemap
echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
echo '<urlset xmlns="">' . PHP_EOL;

while ($row = $result->fetch_assoc()) {
    echo '<url>' . PHP_EOL;
    echo '  <loc>' . htmlspecialchars($row['url']) . '</loc>' . PHP_EOL;
    echo '  <lastmod>' . date('Y-m-d', strtotime($row['last_modified'])) . '</lastmod>' . PHP_EOL;
    echo '</url>' . PHP_EOL;

echo '</urlset>' . PHP_EOL;

// Close the database connection

In this script, make sure to replace the database connection details with your own. This script assumes that you have a table called “urls” with columns “url” and “last_modified”.

Step 2: Set up a .htaccess file to route requests to the PHP script

To make your sitemap available at the expected URL (e.g., ““), create an .htaccess file in your website’s root directory with the following content:

# Apache .htaccess
RewriteEngine On
RewriteRule ^sitemap\.xml$ generate_sitemap.php [L]

This rule will redirect requests for “sitemap.xml” to the “generate_sitemap.php” script we created earlier.

Step 3: Test the sitemap

Now, you can test your dynamic sitemap by navigating to “” in your web browser or by using a sitemap validator tool. You should see an XML sitemap containing the URLs fetched from your database.


In this blog post, we discussed how to generate a dynamic sitemap using PHP. This approach allows you to keep your sitemap up-to-date with the latest content on your website, making it easier for search engines to crawl and index your site. Remember to submit your sitemap to search engines like Google and Bing to ensure they can discover your content.

Got question?

Submit it here

© All rights reserved.