Friday, January 14, 2022
HomeTECH NEWSHow to create multiple XML sitemaps in PHP Sites in 2021

How to create multiple XML sitemaps in PHP Sites in 2021

I will show you how to create editable XML sitemaps using PHP, but first, you should know that you can submit up to 50,000 URLs per XML sitemap in Google. To efficiently produce and maintain a large site with thousands of products pages and product variations it can become burdensome to manually create an editable XML sitemap on your own every few weeks or months.

You likely know that submitting an XML sitemap to Google Webmaster tools is the fastest way to add a page in Google search and tell Google about every page of your website. This allows you to quickly register millions of pages in just one week.

How to map a dynamic XML site and submit it to Google Webmaster for indexing, must-read…

 

Now that you’ve successfully created your dynamic XML site map, it’s time to create your sitemap index file. This way all of your dynamic sitemaps can be linked so that you can submit this single XML file in Google Webmaster Tools and the entire list will automatically appear in your account. In the example, you’ve seen how there are books listed online and on your script, but you can only have up to 50,000 in one XML sitemap so for any long lists of products or categories, like with books, for example, break it down into 25,000 chunks on each individual sitemap.

For example

Allsitemap.php

 

<?php

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=’http://www.sitemaps.org/schemas/sitemap/0.9′>”;

for($i=0;$i<=$count;$i+=25000) {

$output .= “<sitemap>”;

$output .=    “<loc>http://example.com/books/sitemap.php?limit=”.$i.”</loc>”; // location of sitemaps

$output .= “</sitemap>”;

}

echo $output .= “</sitemapindex>”;

exit;

 

The code will create multiple sitemaps with 25,000 URLs. Assuming you have 1 million products in your database, it will create 40 such sitemaps.

http://example.com/books/sitemap.php?limit = 0;

http://example.com/books/sitemap.php?limit = 25000;

http://example.com/books/sitemap.php?limit = 50000;

http://example.com/books/sitemap.php?limit = 100000;

well, and so on

http://example.com/books/sitemap.php?limit = 900000;

http://example.com/books/sitemap.php?limit = 950000;

Sitemap.php

 

<?php  header(‘Content-type: application/xml’);  $baseurl = “http://example.com/books/”;  $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); // Replaces all spaces with hyphens.    return preg_replace(‘/[^A-Za-z0-9\-]/’, , $string); // Removes special chars.}   $output = ‘<?xml version=”1.0″ encoding=”UTF-8″?>’ . \n;  $output .= ‘<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9″>’ . \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’])) { ?> <url>  <loc><?php echo $baseurl.clean(trim($resultSet[‘name’])).‘/’. clean(trim($resultSet[‘author’])).‘/’.$resultSet[‘id’]; ?></loc></url><?php }  } ?></urlset>

you can submit a single sitemap file to Google which will automatically submit the URLs for all of your book items within. You won’t have to create any additional sitemaps, and you only need to upload this single template file. It will generate multiple site maps with 25,000 URLs each.

 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments