对于magento,有不少url是没有加入到sitemap中的,譬如tag,如果我们把tag的url重写成tag/tagname的样子,我们在后台生成sitemap的时候想要把重写后的tagurl加入,就要重写magento的sitemap生成机制了!

下面是步骤:

1.在模块对应的config.xml中的model标签中加入代码:

  <sitemap>
                <rewrite>
                    <sitemap>yourpackagename_exname_Model_Sitemap</sitemap>
                </rewrite>
            </sitemap>

然后这个文件继承extends Mage_Sitemap_Model_Sitemap
下面是写入tag的代码

 /**
         * Generate tag pages sitemap
         */
        $changefreq = (string)Mage::getStoreConfig('sitemap/page/changefreq');
        $priority   = (string)Mage::getStoreConfig('sitemap/page/priority');
        $tags = Mage::getModel('tag/tag')->getPopularCollection()
                ->joinFields(Mage::app()->getStore()->getId())
                
                ->load()
                ->getItems();

        foreach ($tags as $item) {
            $tagname = $item->getName();
            $tagname = str_replace("-","--",$tagname);
            $tagname = str_replace(" ","-",$tagname);

            $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
                htmlspecialchars($baseUrl."tag/".$tagname),
                $date,
                $changefreq,
                $priority
            );
            $this->sitemapFileAddLine($xml);
        }
        unset($tags);

然后在后台重新生成,就可以看到tag的url了!