I am getting a product collection of a certain category on a page in a few different Magento sites. My code for getting the collection is:

        $category = new Mage_Catalog_Model_Category();
        $collection = $category->getProductCollection();
        $collection->addAttributeToFilter('status', 1);
        $collection->addFieldToFilter(array(array('attribute'=>'visibility', 'neq'=>"1" )));

        foreach ($collection as $shopProduct) :

            echo $shopProduct->getProductUrl();


My problem is that on one of the Magento sites we're running the ProductUrl() being fetched is a URL like http://www.my site.com/catalog/product/view/id/2309/s/shopcat/category/373/ and not more like http://www.site.com/shopcat/product-url-key.html. But on all other sites it's appearing as we want it.

Does anyone know why this might be? Thanks! I tried using getUrlPath() too but this didn't return anything. I know I can get round this by doing something like <?php echo $this->getBaseUrl().$shopProduct->getUrlKey().".html"; ?> but that method seems a bit inefficient!

EDIT 21/03/14: I'm still having this problem. I've realised getProductUrl() retrieves the wanted URL on some template files of the site but not others. For example, I'm loading one collection on the homepage and it's giving me the URLs I want. But getProductUrl() is not giving me the URLs I want with the same code on a category view.

  • Did you try to reindex "Index URL rewrites"? Feb 5, 2014 at 10:35
  • yes Please Reindex your data and remove cache
    – Keyul Shah
    Feb 5, 2014 at 10:37
  • I have tried both. I have reindexed in Index Management and I have flushed the cache, the cache was disabled anyway.
    – Sarah
    Feb 5, 2014 at 16:49
  • Enter admin->catalof->url rewrite management. Are there url rerites? If yes try to filter the grid: Target path->[category/some_category_id]
    – mageUz
    Feb 5, 2014 at 21:01
  • Very sorry for the delay in reply. There is a URL rewrite set up for the product in question, so if you go to site.com/shopcat/product-url-key.html or site.com/catalog/product/view/id/2309/s/shopcat/category/373 you still get to the same product page, it's just that the foreach loop is pulling through the wrong type of URL for some reason.
    – Sarah
    Feb 18, 2014 at 16:45

2 Answers 2


Try to get the collection like this:

$collection = $category->getProductCollection();
$collection->addAttributeToFilter('status', 1);
$collection->addFieldToFilter(array(array('attribute'=>'visibility', 'neq'=>"1" )));
//Where the magic happens
//this will add the url rewrite.
//addUrlRewrite can also be left without a parameter to generate url without category.

In other words, let the model know to give the url key instead of the long ugly url with $collection->addUrlRewrite();.

  • question - why do you need the $category->getId() in addUrlRewrite? I see that it works either way (eg even without it). thanks!
    – Ronen Ness
    Apr 17, 2016 at 8:35
  • 2
    Hi @Ness , it depends if you have "Use Category paths for Product URLs turned on" in System / Configuration / Catalog / Catalog / Search Engine Optimizations. If you don't, you can ignore this and just use addUrlRewrite(). If you have category paths turned on, passing the category ID into the function means the product URL that gets rendered is the product URL prepended by the category path.
    – Sarah
    May 31, 2016 at 14:09
  • @Marius Yeah it works for me. Jan 21, 2019 at 6:57

Getting A Products URL

Potentially confusing due to the 3 methods you could use, all of which are in Mage_Catalog_Model_Product:

public function getUrlPath($category=null)
public function getUrlInStore($params = array())
public function getProductUrl($useSid = null)

The best way to explain is to simply show the results of several calls. Given a product whose URL key is mondrian-large-coffee-table-set-multicolour on the domain of http://made.local the results are:



// you cannot stop this method adding ___store to the URL, even by setting _store_to_url to false

// you cannot stop this method adding ___store to the URL, even by setting _store_to_url to false
// note - see the "using _ignore_category" section below for an arguable bug with using this param
$product->getUrlInStore(array('_ignore_category' => true));



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.