Magento 1 Sitemap – How to Change Home in sitemap.xml

cms-pagesmagento-1sitemaps

On our Magento store the sitemap which Magento automatically generates from the admin panel at Catalog > Google Sitemap adds the home url to our homepage like:

<url>
    <loc>http://our-domain.com/home</loc>
    <lastmod>2014-11-17</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.2</priority>
</url>

Is there a way to change it to

<url>
    <loc>http://our-domain.com/</loc>
    <lastmod>2014-11-17</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.2</priority>
</url>

Best Answer

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'] = '';
}
Related Topic