Sitemap.xml change home
you are getting that url because the homepage is a CMS page.
In your case it has the identifier home
.
In order to change that url you need to rewrite the method Mage_Sitemap_Model_Resource_Cms_Page::_prepareObject
and make it look like this:
protected function _prepareObject(array $data)
{
$object = new Varien_Object();
$object->setId($data[$this->getIdFieldName()]);
//for home set url to ''
if ($data['url'] == 'home') {
$data['url'] = '';
}
$object->setUrl($data['url']);
return $object;
}
of course this won't work if you change the homepage to an other page. But it's a quick way of doing it.
If you want the clean version you have to check what is the hompage for your current store.
For this add a new member and method like this:
protected $_homeId = array();
public function getHomepageId($storeId)
{
if (!isset($this->_homeId[$storeId]))) {
$pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_HOME_PAGE, $storeId);
$delimeterPosition = strrpos($pageId, '|');
if ($delimeterPosition) {
$pageId = substr($pageId, 0, $delimeterPosition);
}
$this->_homeId[$storeId] = $pageId;
}
return $this->_homeId[$storeId];
}
In this case, you need to modify the getCollection
method in the same class. Before $page = $this->_prepareObject($row);
add this:
if ($row[$this->getIdFieldName()] == $this->getHomepageId($store)) {
$row['url'] = '';
}
Magento 1.9.0.0. added two events to adjust product and category XML sitemap:
sitemap_categories_generating_before
sitemap_products_generating_before
In Mage_Sitemap_Model_Sitemap::generateXml()
they replaced
foreach ($collection as $item) {
with
$priority = (string)Mage::getStoreConfig('sitemap/product/priority', $storeId);
$collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId);
$products = new Varien_Object();
$products->setItems($collection);
Mage::dispatchEvent('sitemap_products_generating_before', array(
'collection' => $products
));
foreach ($products->getItems() as $item) {
Same for category pages, but 20 lines below they just kept foreach ($collection as $item)
for CMS pages. I'd would adapt this method and rewrite/modify Mage_Sitemap_Model_Sitemap
to replace it with ...
$pages = new Varien_Object();
$pages->setItems($collection);
Mage::dispatchEvent('sitemap_cms_pages_generating_before', array(
'collection' => $pages,
'store_id' => $storeId
));
foreach ($pages->getItems() as $item) {
Then you can use that new event to modify your CMS pages sitemap:
Event:
<sitemap_cms_pages_generating_before>
<observers>
<rename_home>
<class>[model]/observer</class>
<method>renameCmsHomePageUrl</method>
</rename_home>
</observers>
</sitemap_cms_pages_generating_before>
Observer:
public function renameCmsHomePageUrl(Varien_Event_Observer $observer)
{
$collection = $observer->getCollection();
foreach ($collection->getItems() as $item) {
if ($item->getUrl() === 'home') {
$item->setUrl('');
break;
}
}
}
Thanks for the code.
Using this snippet, I have created an extension to automate this process. This will work with mult-store/site configurations too. In this extension, I have updated the CMS Sitemap link generation like the follows:
/**
* Generate cms pages sitemap
*/
$homepage = (string)Mage::getStoreConfig('web/default/cms_home_page', $storeId);
$changefreq = (string)Mage::getStoreConfig('sitemap/page/changefreq', $storeId);
$priority = (string)Mage::getStoreConfig('sitemap/page/priority', $storeId);
$collection = Mage::getResourceModel('sitemap/cms_page')->getCollection($storeId);
foreach ($collection as $item) {
/* Customizing sitemap generation where url key is home */
Mage::log($item->getUrl(), NULL, 'SeoSitemap.log');
$url = $item->getUrl();
if ( $item->getUrl() == $homepage) {
$url = '';
}
$xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
htmlspecialchars($baseUrl . $url),
$date,
$changefreq,
$priority
);
$io->streamWrite($xml);
/* Customization ends */
}
unset($collection);
$io->streamWrite('</urlset>');
$io->streamClose();
$this->setSitemapTime(Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s'));
$this->save();
return $this;
You can find the entire source-code from my repository