Home > Magento SEO > Optimise Magento XML Sitemap

Optimise Magento XML Sitemap

Posted on: 9th Aug 2010 By: Adam Moss 8 Comments

The automatic XML sitemap of Magento has been documented on several occasions as a useful tool for Magento SEO. One thing that’s always bugged me about it however is the fact that by default it indexes the homepage as http://www.yourdomain.com/home rather than just http://www.yourdomain.com. This can be fixed pretty easily because the sitemap is configured by one simple PHP file in the core.

1. Open app> code > core > Mage > Sitemap > Model > Sitemap.php duplicate this locally to create app> code > local > Mage > Sitemap > Model > Sitemap.php

Find the line of code which should be on line 176 which contains the foreach statement. It’s just a simple if statement that checks the variable brought back by $item->getUrl(). If the value is not “home”, run the normal script. If it is “home” it removes the baseUrl extension of . $item->getUrl(). Below is the finished code, I have marked in bold below are the parts that I have added.

foreach ($collection as $item) {
if ($item->getUrl() != "home") {
$xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
htmlspecialchars($baseUrl . $item->getUrl()),
$date,
$changefreq,
$priority
);
} else {

$xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>', 
htmlspecialchars($baseUrl),
$date,
$changefreq,
$priority
);
}
$io->streamWrite($xml);
}

Thanks for reading the Magento blog at ecommerce web design, hope this helps everyone with their Magento SEO.

8 Responses to “ Optimise Magento XML Sitemap ”

  1. Craig
    #1 | 13th December 2010

    Adam,

    Thanks for this useful tip! One question… what do you mean “duplicate this locally”?

    Thanks,
    Craig

  2. Craig
    #2 | 13th December 2010

    Adam,

    Very important point. Excellent post and works like a charm!

    Thanks,
    Craig

  3. Adam Moss
    #3 | 15th December 2010

    Hi Craig, by duplicate locally I mean that you need to create a copy of the file in your app/code/local folder.

    For example, if your file was:
    app/code/core/Mage/Template/Navigation.php

    you’d create:
    app/code/local/Mage/Template/Navigation.php

    The local copy of the file will be used instead of the core copy!

  4. John
    #4 | 24th February 2011

    Hi, thanks for the article. I’ve just changed my Sitemap.php, regenerated the sitemap, but the change doesn’t show up? Am I missing something?

  5. Anon
    #5 | 23rd July 2011

    Same here… no changes when using new code in local or even changing the original Sitemap.php (Magento 1.4.1.1)

  6. Houston
    #6 | 17th August 2011

    It works like a charm. If you use Compilation, make sure you recompile the site and regenerate the sitemap to see the changes. Thank you!

  7. Magento
    #7 | 24th November 2011

    Great job! Adam! it just solves my question about Magento Sitemap.

  8. georg
    #8 | 4th February 2012

    thanks, works like a charm.

    P.S. to make changes take effects, i had to “run compilation process” (system->tools->compilation)

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.