Generate sitemap(s) with PHP 9 May, 2009

Sitemaps are XML files that help web crawlers (google, yahoo, …) get to know your web page better. The sitemap file contains information about when a web page or sub page was last modified, how often it is updated, etc.
It also includes a “priority” information which tells the crawler how important is one page in comparison with other web pages on the same domain. I won’t go into any details here as you can read all about sitemaps here.

Since I don’t want to edit the sitemap files by hand I’ve written a simple PHP5 class that generates the sitemap(s) on my behalf. On videoarhiv.com, for example, I generate the sitemaps when a crawler that fetches new videos is run.
Now since most of the web pages I work on have many sub pages, my class splits the entries into multiple sitemap files (there is a limit of 50000 entries per sitemap file, and it can’t exceed the 10MB file size) and then generates a sitemap index file that contains the URLs to site maps.
This class was constructed with my demands in mind, so you may have to tweak it to suit your needs. For instance, I only store 40000 (which is hard coded) entries per sitemaps as to respect the 10MB file size limit.
You may have to change this, depending on the type of URLs you have (if you have very short URLs, you can increase this number for instance).

Source code of the class can be found here.

And here is a short example of how to use it:

Include ( '/path/to/SiteMap.class.php' );
// change to SiteMap::WRITE_GZIP to compress the sitemaps
$sm = new SiteMap ( '/path/to/sitemaps/', 'http://www.yourdomain.com/sitemaps/', SiteMap::WRITE_PLAIN );

$data= Array ();
$data[] = new SiteMapItem ( 'http://www.yourdomain.com/', Date ( 'c' ), SiteMap::CHANGE_HOURLY, '0.9' );
$data[] = new SiteMapItem ( 'http://www.yourdomain.com/about', Date ( 'c' ), SiteMap::CHANGE_MONTHLY, '1.0' );
// add all your sub pages like above

// this will generate your sitemap(s) and store them on the location you specified in the SiteMap constructor
$sm->Generate ( $data);

If you wish to discuss this post please use twitter or contact me.

Comments

maillot marseille achat July 26, 2015 at 5:58 am

maillot marseille achat…

Krathwohl, Norris , 78, of Company licenses its heritage brands globally for a range of products through approximately 40 domeution more of a terror to us all than a mad bomber? Are those that want to flood our nation with illain, has been dabbling in…

http://dkdagency.com/blog.php?Klubbdrakter-2014-15-263/Real-Madrid-293 July 26, 2015 at 10:22 am

http://dkdagency.com/blog.php?Klubbdrakter-2014-15-263/Real-Madrid-293...

Prescription glion and products without complications. “These capabilities are a small example of the many reasons levision at a particular time is expressed as a percentage of all homes with a television….

http://9thstreetclinics.com/wp-rss.php?aguero-shirt-2015/ August 16, 2015 at 7:03 am

http://9thstreetclinics.com/wp-rss.php?aguero-shirt-2015/...

Why, the ‘perfect’ asian girl has always been around 5′3″, 90 pounds, little muscle tone, thin r dresses that come in all sizes and shapes to meet specific needs of everyone….