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.

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

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


  header('Content-type: application/xml');
  $baseurl = "";
  $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="">' . "\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'])) { ?>
  <loc><?php echo $baseurl.clean(trim($resultSet['name'])).'/'. clean(trim($resultSet['author'])).'/'.$resultSet['id']; ?></loc>
<?php }  } ?>

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.

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

5 Replies to “How to create dynamic XML sitemap and submit to google web master for indexing”

  1. Hello rohit,

    Can you please suggest or provide an example of,that how can add ‘pay per click’-functionality on my website(for fully PHP,HTML5,CSS3,Javascript,Bootstrap3 based website).I want earn from my website like blogging… Please help me..

    my email I.D :- [email protected]

    Thanks in advance…

  2. Go for google AdSense best PPC network available apply for the same if you got approved place ads on your blog and start montizing , or you can choose some other network too like infolinks or revenuehits, Best of luck

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.