0

Currently, the XML Sitemap is having unwanted duplicate URLs which are blog URLs with the common folder path /index.php/blog/

Although, the /index.php/ is blocked in robots.txt. We are not able to edit the existing sitemap in Magento CMS.

How to remove only these URLs (with /index.php/blog/) from the XML sitemap?

0

You can create custom module to extend magento core sitemap functionality.

You need to overrite app\code\core\Mage\Sitemap\Model\Sitemap.php file and need to overwrite below function

Original function

public function 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 categories sitemap
     */
    $changefreq = (string)Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
    $priority   = (string)Mage::getStoreConfig('sitemap/category/priority', $storeId);
    $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
    $categories = new Varien_Object();
    $categories->setItems($collection);
    Mage::dispatchEvent('sitemap_categories_generating_before', array(
        'collection' => $categories
    ));
    foreach ($categories->getItems() as $item) {
        $xml = sprintf(
            '<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
            htmlspecialchars($baseUrl . $item->getUrl()),
            $date,
            $changefreq,
            $priority
        );
        $io->streamWrite($xml);
    }
    unset($collection);

    /**
     * Generate products sitemap
     */
    $changefreq = (string)Mage::getStoreConfig('sitemap/product/changefreq', $storeId);
    $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) {
        $xml = sprintf(
            '<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
            htmlspecialchars($baseUrl . $item->getUrl()),
            $date,
            $changefreq,
            $priority
        );
        $io->streamWrite($xml);
    }
    unset($collection);

    /**
     * Generate cms pages sitemap
     */
    $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) {
        $xml = sprintf(
            '<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
            htmlspecialchars($baseUrl . $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;
}

Override function

public function 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 categories sitemap
     */
    $changefreq = (string)Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
    $priority   = (string)Mage::getStoreConfig('sitemap/category/priority', $storeId);
    $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
    $categories = new Varien_Object();
    $categories->setItems($collection);
    Mage::dispatchEvent('sitemap_categories_generating_before', array(
        'collection' => $categories
    ));
    foreach ($categories->getItems() as $item) {
        $xml = sprintf(
            '<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
            htmlspecialchars($baseUrl . $item->getUrl()),
            $date,
            $changefreq,
            $priority
        );
        $io->streamWrite($xml);
    }
    unset($collection);

    /**
     * Generate products sitemap
     */
    $changefreq = (string)Mage::getStoreConfig('sitemap/product/changefreq', $storeId);
    $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) {
        $xml = sprintf(
            '<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
            htmlspecialchars($baseUrl . $item->getUrl()),
            $date,
            $changefreq,
            $priority
        );
        $io->streamWrite($xml);
    }
    unset($collection);

    /**
     * Generate cms pages sitemap
     */
    $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) {
      if(!strstr($item->getUrl(),"/blog"){
        $xml = sprintf(
            '<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
            htmlspecialchars($baseUrl . $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;
}
0

It's better to create a separate module for this,

For example YourNamespache_Sitemap with a proper rewrite of the model Mage_Sitemap_Model_Sitemap there.

For detail code reference :

Want to remove a specific url from Magento sitemap.xml?

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