Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Sitemap / Canonical Product URL issues
 
vegetav
Jr. Member
 
Total Posts:  13
Joined:  2011-10-02
 

I’m trying to get my head around an annoying issue I’m facing, and despite much research haven’t been able to find a solution yet. It is related to the Google Sitemap, canonical URLs and category structure.

The site in question has a very basic single level category structure, with each product only being in one category as follows:

- Category 1
- Product 1
- Product 2
- Product 3

- Category 2
- Product 1
- Product 2
- Product 3

This is what I’m trying to achieve:
- The on site product URLs to be included within the product URL
- Use category URLs in canonical tags to ensure Google indexes them
- Use category URLs in my XML Google Sitemap generated by Magento

I would have though this would be simple, but I just can’t seem to get this setup. I wouldn’t be so fussed about it, but I believe that it’s better for SEO to provide category paths (at least if the product is only in one category anyway), and with social media being so essential now, I’m finding issues where if people ‘like’ a product on Facebook which they have accessed via the category on the site, when somebody finds the page on Google (which is indexed with no category path), the product appears to have no ‘likes’ etc.

Has anybody had this same problem and / or found a solution?

 
Magento Community Magento Community
Magento Community
Magento Community
 
vegetav
Jr. Member
 
Total Posts:  13
Joined:  2011-10-02
 

I’ve managed to answer my own question. For anyone else who is struggling, the answer is to extend the Mage_Sitemap_Model_Sitemap model and add your own generateXml() function. You only need to replace the ‘generate products sitemap’ section as below.

This will check each product to see if it is in a category, and if so, loop through the categories and add them to the sitemap (providing the use category path in product URL option is selected in the config)

/**
         * 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);
        foreach (
$collection as $item{
            $_product 
Mage::getModel('catalog/product')->load($item->getId());
            
$_categoryIds $_product->getCategoryIds();

            if(
Mage::getStoreConfig('catalog/seo/product_use_categories') && $_categoryIds $_product->getCategoryIds()) {
                
foreach($_categoryIds as $_categoryId{
                    $_category 
Mage::getModel('catalog/category')->load($_categoryId);
                    
$url htmlspecialchars($baseUrl $_product->getUrlPath($_category));

                    
$xml sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
                        
$url,
                        
$date,
                        
$changefreq,
                        
$priority
                    
);
                    
$io->streamWrite($xml);
                
}
            }
            
else {
                $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);
 
Magento Community Magento Community
Magento Community
Magento Community
 
aap411vm
Jr. Member
 
Total Posts:  2
Joined:  2012-04-25
 

Thanks for the code! But the weird thing is that it does not read the changes of the file..

Should this be the right location?: app/code/core/Mage/Sitemap/Model/Sitemap.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
RageGuy
Jr. Member
 
Total Posts:  5
Joined:  2013-03-29
 
aap411vm - 04 June 2013 11:32 PM

Thanks for the code! But the weird thing is that it does not read the changes of the file..

Should this be the right location?: app/code/core/Mage/Sitemap/Model/Sitemap.php

same problem here, too.

Where i have to change the file?

can you post the exact path, please?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top