We recently launched a website where site visitors can write reviews. Each new review is displayed on its own page, which is generated dynamically from the database. I automated the site map for the site, so that it would be updated whenever a new review was added to the site.
Step 1
Download and configure Google’s site map generator. Google’s instructions are clear and easy to follow, so I won’t repeat them here.
Step 2
Create a script to generate the urls needed by the generator to create the map.
Google’s generator offers five methods for providing urls to the generator. Because the urls for this site are not related to the directory and file structure, I chose to use the urllist method. Something like the following script, which can be run from the command line, could be used to generate the list:
query($query)) {
$row = $result->fetch_row();
$pages = ceil($row[0]/10);
for($i = 1; $i <= $pages; $i++) {
fwrite($fh, "http://www.site_name.com/archives/index?page=".$i."\n");
}
}
// add urls for the pages for individual reviews
$query2 = "SELECT id from reviews where draft=0";
if ($result2 = $db->query($query2)) {
while ($row2 = $result2->fetch_assoc()) {
fwrite($fh, "http://www.site_name.com/reviews/".$row2['id']."\n");
}
}
fclose($fh);
$db->close();
?>
I could have added the above code directly to my app, but wanted the flexibility easily to run it from the command line should the need ever arise.
Step 3
Add the code to your application to run the script created in step 2 and then run Google’s generator script. I added the following method to my app, which is called each time a new review is saved.
public static function updateSitemap() {
exec('php /path_to_script/urllist-generator.php');
exec('python /path_to_sitemap_generator/sitemap_gen.py --config=/path_to_config_file/config.xml');
}
The method regenerates the list of urls, then executes the sitemap generator to create an updated site map.



