The steps I ended up using were as follows, and the comments and answers so far got me started in the right direction.
First I added a row to the "sitemap" table. Since we have multi-store set up, and because I want to keep my module store agnostic, I didn't hard code this INSERT into a MySQL migration, but just ran it on the store manually:
INSERT INTO sitemap (sitemap_type, sitemap_filename, sitemap_path, store_id)
VALUES ('people', 'people.xml', '/sitemap/', 2);
Then I overwrote the Mage_Sitemap_Model_Sitemap
model inside the global/models section in my own module's config.xml file:
<global>
<models>
<sitemap>
<rewrite>
<sitemap>Mymod_People_Model_Sitemap</sitemap>
</rewrite>
</sitemap>
</models>
</global>
This overwrites any calls to Mage_Sitemap_Model_Sitemap
site-wide with my custom model, but I didn't want to copy and paste too much code there. Using Petar Dzhambazov's suggestion, I used a conditional to defer to the parent class unless the sitemap_type
is "people".
class Mymod_People_Model_Sitemap extends Mage_Sitemap_Model_Sitemap
{
const PAGE_REFRESH_FREQUENCY = 'weekly';
const PAGE_PRIORITY = '1.0';
public function generateXml()
{
if ($this->getSitemapType() != 'people') {
return parent::generateXml();
}
$io = new Varien_Io_File();
$io->setAllowCreateFolders(true);
$io->open(array('path' => $this->getPath()));
if ($io->fileExists($this->getSitemapFilename()) && !$io->isWriteable($this->getSitemapFilename())) {
Mage::throwException(Mage::helper('sitemap')->__('File "%s" cannot be saved. Please, make sure the directory "%s" is writeable by web server.', $this->getSitemapFilename(), $this->getPath()));
}
$io->streamOpen($this->getSitemapFilename());
$io->streamWrite('<?xml version="1.0" encoding="UTF-8"?>' . "\n");
$io->streamWrite('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
$storeId = $this->getStoreId();
$date = Mage::getSingleton('core/date')->gmtDate('Y-m-d');
$baseUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
/**
* Generate people sitemap
*/
$changefreq = Mymod_People_Model_Sitemap::PAGE_REFRESH_FREQUENCY;
$priority = Mymod_People_Model_Sitemap::PAGE_PRIORITY;
$collection = Mage::getModel('people/person')->getCollection();
foreach ($collection as $item) {
$xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
htmlspecialchars($item->getUrl()),
$date,
$changefreq,
$priority
);
$io->streamWrite($xml);
}
unset($collection);
$io->streamWrite('</urlset>');
$io->streamClose();
$this->setSitemapTime(Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s'));
$this->save();
return $this;
}
}
Is there a better way, that avoids copying and pasting so much from the parent class?
To address one of your initial comments:
"I have set Product URL Suffix(html) and Category URL Suffix(html) in the configuration.
This suffix are not showing in sitemap.xml."
We noticed this bug introduced in EE 1.13. We requested a patch and received the following. I don't believe this is EE specific so hopefully it helps a bit. Also, just upgraded a EE 1.12 site to 1.14 and noticed all the .html extension were dropped from sitemap.xml as well. Yes, having .html is old school and really not of any seo value these days but this bug can really suck if it goes unnoticed.
Warning ... I did not look at the referenced file in community. Use this as a guide, do not try to apply the patch blindly to your community store. Or if you do, at least test it very thoroughly before going live.
__PATCHFILE_FOLLOWS__
diff --git app/code/core/Mage/Sitemap/Model/Sitemap.php app/code/core/Mage/Sitemap/Model/Sitemap.php
index 2213a4f..734bb91 100644
--- app/code/core/Mage/Sitemap/Model/Sitemap.php
+++ app/code/core/Mage/Sitemap/Model/Sitemap.php
@@ -151,11 +151,13 @@ class Mage_Sitemap_Model_Sitemap extends Mage_Core_Model_Abstract
*/
$changefreq = (string)Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
$priority = (string)Mage::getStoreConfig('sitemap/category/priority', $storeId);
+ $urlSuffix = Mage::helper('catalog/category')->getCategoryUrlSuffix($storeId);
+ $urlSuffix = ($urlSuffix) ? '.' . $urlSuffix : '';
$collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
foreach ($collection as $item) {
$xml = sprintf(
'<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
- htmlspecialchars($baseUrl . $item->getUrl()),
+ htmlspecialchars($baseUrl . $item->getUrl() . $urlSuffix),
$date,
$changefreq,
$priority
@@ -169,11 +171,13 @@ class Mage_Sitemap_Model_Sitemap extends Mage_Core_Model_Abstract
*/
$changefreq = (string)Mage::getStoreConfig('sitemap/product/changefreq', $storeId);
$priority = (string)Mage::getStoreConfig('sitemap/product/priority', $storeId);
+ $urlSuffix = Mage::helper('catalog/product')->getProductUrlSuffix($storeId);
+ $urlSuffix = ($urlSuffix) ? '.' . $urlSuffix : '';
$collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId);
foreach ($collection as $item) {
$xml = sprintf(
'<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
- htmlspecialchars($baseUrl . $item->getUrl()),
+ htmlspecialchars($baseUrl . $item->getUrl() . $urlSuffix),
$date,
$changefreq,
$priority
Best Answer
You need to create rewrites in
.htaccess
:How to add sitemap.xml for multistore magento