›Create Google Sitemap in Php and Mysql
Google Xml Sitemap in Php and Mysql, In this tutorial i will teach you how to create your own Php and Mysql Sitemap for your website.
What is Sitemap?
Sitemaps are a way to tell Google about pages on your site we might not otherwise discover. In its simplest terms, a XML Sitemap—usually called Sitemap, with a capital S—is a list of the pages on your website. Creating and submitting a Sitemap helps make sure that Google knows about all the pages on your site, including URLs that may not be discoverable by Google’s normal crawling process.
Sitemaps are particularly helpful if:
— Your site has dynamic content.
— Your site has pages that aren’t easily discovered by Googlebot during the crawl process—for example, pages featuring rich AJAX or images.
— Your site is new and has few links to it. (Googlebot crawls the web by following links from one page to another, so if your site isn’t well linked, it may be hard for us to discover it.)
— Your site has a large archive of content pages that are not well linked to each other, or are not linked at all.
Google doesn’t guarantee that we’ll crawl or index all of your URLs. However, we use the data in your Sitemap to learn about your site’s structure, which will allow us to improve our crawler schedule and do a better job crawling your site in the future. In most cases, webmasters will benefit from Sitemap submission, and in no case will you be penalized for it.
Google adheres to Sitemap Protocol 0.9 as defined by sitemaps.org. Sitemaps created for Google using Sitemap Protocol 0.9 are therefore compatible with other search engines that adopt the standards of sitemaps.org.
File format
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://example.com/</loc>
<lastmod>2006-11-18</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
</urlset>
Element Definations
Click here to view the full Element definitions detail
Lets Create Php Google Sitemap
First we have to make our map skeleton with some needed stuff (for example database connection), Create a new “sitemap.php” file and copy and paste the below code:
$host = "localhost"; // host name $user = "user"; // database user name $pass = "password"; // database password $database = "dbname"; // database name // connecting to database $connect = @mysql_connect($host,$user,$pass)or die (@mysql_error()); // selecting database @mysql_select_db($database,$connect) or die (@mysql_error());
$host: leave it default
$user: change it to your database username
$pass: change it to your database password
$database: change it to your database name
Creating XML
Now lets add the default headers to your “sitemap.php”, copy and paste the below code under the database connecting and selecting code:
// default header(don't delete)
header("Content-Type: text/xml;charset=iso-8859-1");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
Adding Urls to XML
Here we have simple MySQL query (we take newest articles url and date from “mytable” table), Copy & Paste the below code under the default headers:
// selecting data from "mytable"
// mytable = your article table name
$query = @mysql_query("SELECT * FROM mytable");
while($row = @mysql_fetch_array($query)){
// [url] = article url
$url = $row['url'];
// [time] = article date
$date = date("Y-m-d", $row['time']);
// NO CHANGES BELOW
echo
'<url>
<loc>' . $url .'</loc>
<lastmod>'. $date .'</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
';
}
echo '</urlset>';?>
$row['url']; Change this to your article url.
$row['date']; Change this to your article date.
and that’s it we are finished!
Putting it all together
<?php
$host = "localhost"; // host name
$user = "user"; // database user name
$pass = "password"; // database password
$database = "dbname"; // database name
// connecting to database
$connect = @mysql_connect($host,$user,$pass)or die (@mysql_error());
// selecting database
@mysql_select_db($database,$connect) or die (@mysql_error());
// default header(don't delete)
header("Content-Type: text/xml;charset=iso-8859-1");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
// mytable = your content table name
$query = @mysql_query("SELECT * FROM mytable");
while($row = @mysql_fetch_array($query)){
// [url] = content url
$url = $row['url'];
// [time] = content date
$date = date("Y-m-d", $row['time']);
// NO CHANGES BELOW
echo
'<url>
<loc>' . $url .'</loc>
<lastmod>'. $date .'</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
';
}
echo '</urlset>';?>
Lets do a little more
do you want to change www.yourwebsite.com/sitemap.php to www.yourwebsite.com/sitemap.xml, it is so easy, just add the below code in your “.htaccess” file:
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule (.*)\.xml(.*) $1.php$2 [nocase] </IfModule>
Once you added the above code in your “.htaccess” file, the www.yourwebsite.com/sitemap.php file will be automatically converted into www.yourwebsite.com/sitemap.xml.
You will have something like this, click here
do you need any help? just reply below and we are ready to help you ……

Great tuts, exactly what i was searching for, thanks! By the way, in your comments form, the text color is the same as the input background color.. you should put the text color to white.
very helpful article. i developed my automated googlemap by this tutorial. thanks.