7

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>

3 Answers 3

9

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'] = '';
}
4
  • so I will need to override app/code/core/Mage/Sitemap/Model/Resource/Cms/Page.php as described here
    – Holly
    Commented Nov 17, 2014 at 12:06
  • @CreedBratton. Yep...that's what I'm saying.
    – Marius
    Commented Nov 17, 2014 at 12:07
  • great - thanks for your help. I have a simlar question here would you mind looking at it?
    – Holly
    Commented Nov 17, 2014 at 12:09
  • @MariusI tried this and found that the value are logging correctly in my custom log file but when I check at mysite.com/sitemap/sitemap.xml, it is still showing mysite.com/home instead of mysite.com. Is there any need for cache clear/compilation run or something like that? Commented Mar 27, 2017 at 9:21
3

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;
        }
    }
}
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.