You are here:  » How to create a categories sitemap ?


How to create a categories sitemap ?

Submitted by shogounou on Thu, 2009-11-26 22:06 in

Hi David

Can youshow us how to create a sitemap including categories ?

Cheers

Guillaume

Submitted by support on Fri, 2009-11-27 09:04

Hello Guillaume,

The /categories/ home page (or categories.php if you are not using rewrite) is effectively the Categories sitemap as it stands - or do you mean an XML sitemap for search engines to add to sitemap.php?

Cheers,
David.

Submitted by shogounou on Sun, 2009-11-29 17:15

Hi David

I would like to say add a sitemap-categories.php in order to submit to Google Webmaster Tools

Cheers

Guillaume

Submitted by support on Mon, 2009-11-30 09:31

Hello Guillaume,

This should do the trick:

sitemap-categories.php:

<?php
  require("includes/common.php");
  header("Content-Type: text/xml");
  print "<?xml version='1.0' encoding='UTF-8'?>";
  print "<urlset xmlns='http://www.google.com/schemas/sitemap/0.84' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd'>";
  $sql = "SELECT DISTINCT(category) AS category FROM `".$config_databaseTablePrefix."products` ORDER BY category";
  $link = @mysql_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword);
  @mysql_select_db($config_databaseName,$link);
  $result = mysql_unbuffered_query($sql,$link);
  $sitemapBaseHREF = "http://".$_SERVER["HTTP_HOST"].$config_baseHREF;
  while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  {
    print "<url>";
    if ($config_useRewrite)
    {
      $sitemapHREF = "category/".urlencode(str_replace(" ","-",$row["category"]))."/";
    }
    else
    {
      $sitemapHREF = "products.php?q=category:".urlencode($row["category"]);
    }
    print "<loc>".$sitemapBaseHREF.$sitemapHREF."</loc>";
    print "</url>";
  }
  print "</urlset>";
?>

Cheers,
David.