Magento – Remove /home url from sitemap and url site structure

homesitemapsurlurl-rewrite

Remarkably simple problem I have that I'm not finding a simple solution for.

Essentially I have a duplicate url issue with a Magento site's home page.

I have in the url structure the below:

www.domain.com

but also:

www.domain.com/home

Which essentially point to the same page.

I want to remove the latter url which has /home at the end.

I can't simply disable the CMS page that is the home page, as the home page then loads without the home page content in there.

All I want to do is essentially remove the /home from my sitemap generation and from Google's search index.

I still need the main url www.domain.com to index and be found though.

I'm finding that Google is not indexing my main url properly because of the duplication.

I would have thought there would be a basic setting in Magento admin to prevent this? Thanks for any help as always.

Best Answer

How to remove home identifier from the homepage url inside the generated sitemap.

(Tested on CE 1.9.3.4)

  • Before: <loc>http://www.example.com/home</loc>

  • After: <loc>http://www.example.com/</loc>


Copy your Sitemap.php from:

app/code/core/Mage/Sitemap/Model/Sitemap.php

to:

app/code/local/Mage/Sitemap/Model/Sitemap.php

Look for:

foreach ($collection as $item) {

and 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 copy Page.php file from:

app/code/core/Mage/Sitemap/Model/Resource/Cms/Page.php

to:

app/code/local/Mage/Sitemap/Model/Resource/Cms/Page.php

and open this too. Look for:

$page = $this->_prepareObject($row);

and REPLACE IT with:

if ($row['url'] == 'home') {
    $row['url'] = '';
}
$page = $this->_prepareObject($row);

Go to the backend, generate a brand new sitemap and Bob's your uncle! :D