How to create dynamic XML sitemap and submit to google web master for indexing

In this tutorial I’ll tell you very important method to generate dynamic xml sitemap for your website and how can we indexed all our dynamic url in google search.

Every body wants to create website now days and generate some traffic fast. So this is the trick for smart geeks, The below script is tested by me and worked great for me.

Let me tell you my experience, I had aprox 2 lakh dynamic urls which i created from my database rocords and i want to index all urls in google search, So i have created a php script which pull records from database and create a dynamic url for each records.



Suppose we have a books table with their name and auther.

ID NAME AUTHOR
1 Book Name-1 Book Author-1
2 Book Name-2 Book Author-2
3 Book Name-3 Book Author-3

My task is to index all my books url with their author name in google search, so that if anybody is looking for same books then he can find me in google search. Google automatically crawl urls but if you want fast result then give it try.

Create file sitemap.php and paste below script after that upload your sitemap.php file in your project root directory, Your sitemap url will be http://www.example.com/sitemap.php

You can make changes in below script according to your need this is just for demonstration purpose.

sitemap.php

<?php
  header('Content-type: application/xml');
  $baseurl = "http://example.com/books/";
  $hostname = "localhost";
$username = "username";
$password = "password";
$dbname = "booksdb";
$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, 50000";
    $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>

Now time to submit your xml sitemap to google web master.
Login into your google web master account and submit your site map
if you don’t know how to submit sitemap in google web master please follow this tutorial.
http://www.iamrohit.in/how-to-make-visible-newly-hosted-websites-in-google-search/
sitemap

If you like this post please don’t forget to subscribe My Public Notebook for more useful stuff.