How to Create Dynamic XML Sitemap in PHP

Sitemaps prefer to be in XML format, but that does not mean that you have to write XML code for each page manually. Let’s build a XML sitemap together with PHP.

sitemap is very play very important role in SEO it represent all URL website. it help to increase ranking of website.

PHP Code

<? xml version = "1.0" encoding = "UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>http://www.example.com/index.php </loc>
        <lastmod>2018-01-23T18:00:15+00:00</lastmod>
    </url>
</urlset>


<?php
header('Content-type: application/xml');
$get_result = mysqli_query($conn, "select * from post");

echo "<?xml version='1.0' encoding='UTF-8'?>" . "\n";
echo "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>" . "\n";

echo "
<url>
 <loc>https://scripts.guru/</loc>
 <lastmod>2019-01-23T18:00:15+00:00</lastmod>
 <changefreq>daily</changefreq>
</url>
<url>
 <loc>https://scripts.guru//bout.php</loc>
 <lastmod>2019-01-23T18:00:15+00:00</lastmod>
 <changefreq>daily</changefreq>
</url>
<url>
 <loc>https://scripts.guru/contact.php</loc>
 <lastmod>2019-01-23T18:00:15+00:00</lastmod>
 <changefreq>daily</changefreq>
</url>";

while ($row = mysqli_fetch_array($get_result)) {
    echo "<url>";
    echo "<loc>" . $row['link'] . "</loc>";
    echo "<lastmod>" . $row['date'] . "</lastmod>";
    echo "<changefreq>daily</changefreq>";
    echo "</url>";
}

echo "</urlset>";
?>

Leave a Reply

Your email address will not be published. Required fields are marked *