
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.example.com/</loc> <lastmod>2005-01-01</lastmod> <changefreq>monthly</changefreq> <priority>0.8</priority> </url> </urlset>
<?php
$query = mysql_query ( "SELECT * FROM articles ORDER BY ID DESC" );
$row = mysql_fetch_assoc ( $query );
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
############# BEGIN LOOP ############
do {
$link = 'http://www.mydomain.com/articles.php?ID=' . $row['ID'];
$lastmod = ( $row['date_updated'] ) ? $row['date_updated'] : $row['date_added'];
$priority = 0.5;
$changefreq = 'monthly';
?>
<url>
<loc><?=$link?></loc>
<lastmod><?=$lastmod?></lastmod>
<priority><?=$priority?></priority>
<changefreq><?=$changefreq?></changefreq>
</url>
<?php
} while ( $row = mysql_fetch_assoc ( $query ) );
############# END LOOP ############
?>
</urlset>
Notice I've used 0.5 as the priority of the pages since we're linking article entries and we're not expecting so much new content on those pages (maybe some comments from time to time). For "lastomd" I've used the last date the article was updated and if it wasn't at all, the date it was added to the database. Everything else is quite easy and self-explanatory.
Don't forget to escape your data, especially on the "loc" tag that holds the url. Here's a PHP function that I often use:
function do_xhtml ( $string ) {
$string = stripslashes ( $string );
$string = str_replace ( "'", "'", $string );
$string = str_replace ( '"', '"', $string );
$string = str_replace ( '?', '-', $string );
$string = str_replace ( '?', ''', $string );
$string = str_replace ( '?', '', $string );
$string = str_replace ( '?', '', $string );
$string = str_replace ( '`', '', $string );
return $string;
}
As you can see, with a few lines of code, we managed to create our own dynamic sitemap that will always be updated whenever our site grows. We no longer have to depend or spend money on other solutions and services.
There are many websites that have a lot of content to link with a sitemap and only one XML file would not be enough. We're talking about thousands of pages that would create a huge sitemap. For this situation, google comes with a solution: a sitemap index that will link to other XML files. Basically, you use more sitemaps to divide your website and then link all of them into a single file. Let's take for example roScripts which has 5 sections: Scripts, Articles, Jobs, Snippets and Resources. Predicting that those segments will enjoy a rapid growth in their records, a good idea would be to create a different sitemap for each section in part and then link all of them using an index. Ok, we'll finish with a sitemap index example that we mentioned above.
<?php echo '<?xml version="1.0" encoding="utf-8"?>' . "\n"; ?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>http://www.mydomain.com/articles_sitemap.php</loc> <lastmod><?=echo date ( 'Y-m-d' )?></lastmod> </sitemap> <sitemap> <loc>http://www.mydomain.com/scripts_sitemap.php</loc> <lastmod><?=echo date ( 'Y-m-d' )?></lastmod> </sitemap> <sitemap> <loc>http://www.mydomain.com/resources_sitemap.php</loc> <lastmod><?=echo date ( 'Y-m-d' )?></lastmod> </sitemap> <sitemap> <loc>http://www.mydomain.com/snippets_sitemap.php</loc> <lastmod><?=echo date ( 'Y-m-d' )?></lastmod> </sitemap> <sitemap> <loc>http://www.mydomain.com/jobs_sitemap.php</loc> <lastmod><?=echo date ( 'Y-m-d' )?></lastmod> </sitemap> </sitemapindex>That's just about everything, remember to go to google webmasters central and add your new sitemap.
Added by roScripts on August-28-2007, 9:12 pm
