How to create multiple XML sitemap in PHP

Here I’ll show you How to create multiple XML sitemap in PHP, As you know you can submit 50,000 URL’s per XML sitemap in google but IF you are working on any product base website and you have 10 lakh products url, And you want to submit al URL’s to google then this method will help you to create dynamic XML sitemap.
You know submitting XML sitemap in google webmaster is quickest way to index your web pages in google search and tells google about each pages of your website. You can easily register laks of pages in just one week by this methods.

First read this article – How to create dynamic XML sitemap and submit to google web master for indexing

Now you have successfully create your dynamic XML site map and ready to create a file where you can link all your dynamic sitemap So that you could submit this single XML file in google web master and it’ll automatically pull all linked sitemaps.

As you have seen in the example you have books database and need to submit 10 lakh books pages in google web master but you can submit only 50,000 URL’s per sitemap, So in below code I’ll get count of books then break all sitemap in 25,000 urls on each sitemap See following example.


  header('Content-type: application/xml');
  $hostname = "localhost";
$username = "username";
$password = "password";
$dbname = "booksdb";
$con = mysqli_connect($hostname, $username, $password, $dbname);
 $query = "SELECT name, author, id FROM books WHERE  status=1";
    $result = mysqli_query($con, $query);
$count = mysqli_num_rows($result);
$output = "<?xml version='1.0' encoding='UTF-8'?>";
  $output .= "<sitemapindex xmlns=''>";
    for($i=0;$i<=$count;$i+=25000) {
  $output .= "<sitemap>";
  $output .=    "<loc>".$i."</loc>"; 
  $output .= "</sitemap>";
  echo $output .= "</sitemapindex>";

Above code will create multiple sitemaps with 25,000 url’s each. Suppose you have 10 lakh books in database then it’ll create 40 sitemap like this.
OUTPUT: = 0; = 25000; = 50000; = 100000;
.. . . . . . . . . . . . . . .
… . . . . . . . . . . . . . .. . and so on = 900000; = 950000;


  header('Content-type: application/xml');
  $baseurl = "";
  $hostname = "localhost";
$username = "username";
$password = "password";
$dbname = "booksdb";
$limit = $_GET['limit'];
$con = mysqli_connect($hostname, $username, $password, $dbname);
 function clean($string) {
   $string = str_replace(' ', '-', $string); 
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); 
  $output = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  $output .= '<urlset xmlns="">' . "\n";
  echo $output;
  <?php $query = "SELECT name, author, id FROM books WHERE  status=1 LIMIT 0, $limit";
    $result = mysqli_query($con, $query);
    $res = array();
    while($resultSet = mysqli_fetch_assoc($result)) { 
 if(!empty($resultSet['name'])) { ?>
  <loc><?php echo $baseurl.clean(trim($resultSet['name'])).'/'. clean(trim($resultSet['author'])).'/'.$resultSet['id']; ?></loc>
<?php }  } ?>

All done now you can submit allsitemap.php single sitemap file in google web master which automatically submit all books urls in google web master, you don’t need to create more sitemaps. Only this single file generate multiple sitemaps with 25000 URL’s each.

Posted in PHP