Using mage for the friendly category url

Just started learning how to use the Mage for retrieving info from the database for a Google base feed. I have been calling the tables directly but I am not able to figure out how to get the "friendly" url.

Direct: /catalog/product/view/id/829/s/double-tier-ventilated-lockers/

Friendly: /gym-lockers/double-tier-ventilated-gym-lockers/double-tier-ventilated-lockers.html

I am using getProductUrl() for the direct url. I read something where you have to walk through the categories and relate the products to it (addUrlRewrite) for the Friendly URL, but with my limited knowledge it isn't making sense to me.

Question: How do I pass a ProductModel into a function of some sort and have it return the friendly url?

Thanks for your help


You need to reindex the product urls. Go to System > Index Management and reindex all the options that require reindexing that should give you friendly urls

For those interested, create a function and pass the entity id into it:

function getProductItemUrl($productId)
     return Mage::getModel('core/url_rewrite')->getCollection()

$producturl = getProductItemUrl($product->getId());
$producturl = Mage::getUrl().$producturl;

The url rewrite part is implemented kind of poorly.
There is this method in the product model

public function getCategoryId()
    if ($category = Mage::registry('current_category')) {
        return $category->getId();
    return false;

This method is somehow involved in determining the product url. This is wrong. The product model should not depend on the context.
But you can create your own method that receives as parameter the product and returns a friendly url like this:

public function getMeANiceUrl($product) 
    $categoryIds = $product->getCategoryIds();
    if (count($categoryIds) == 0) {
        return $product->getProductUrl();
    $oldRegisteredCategory = Mage::registry('current_category');
    $obj = new Varien_Object(array('id' => $categoryIds[0]));
    Mage::register('current_category', $obj);
    $url = $product->getProductUrl();
    if ($oldRegisteredCategory) {
        Mage::register('current_category', $oldRegisteredCategory);
    return $url;

you can write this method in your own helper. There are tons of tutorials on the web on how to create your own helper.

Similar questions

Custom router in Magento-2(SCO Friendly URL)
Anyone knows an alternative way to implement Fancy URL(This won't affect url_rewrite table entries) for Magento 2, as we had for Magento 1 as mentioned here: I need to inject product SKU this way: I've implemented it in one of my M...
How to Enable Search Engine Friendly URL's in Magento2?
I am looking to use SEO friendly urls. What would be the best approach to do so?
making Url user friendly in magento
This is my custom module url I want to call the next action from the controller apiaction with this: but remove index in the url: My config.xml page
Magento 2 Category SEO friendly URLS
I have a Magento 2 website and require some direction with setting up sub-category urls to be SEO friendly. The Navigation menu work just fine i.e Grooming Kit the issue i'm having is with sub-categories in the left sidebar filters which when a specific category is clicked on it displays as following url ...
How to retrieve the Category name, Category URL and Category image using Category-Id?
As we know that in the Category Management page we can upload an image and give a URL for each category. I want to retrieve the Category name, Category URL and Category image using only the Category Id that I already have with me. Is it possible to do so?
How do I retrieve an ajax friendly phtml file?
I have created a custom front end module that allows a user to go through a wizard like process. I would like to make this AJAX. I am loading my layout block with the following However the default template is being returned with that phtml file, how do I get magento to return just the contents of the phtml file?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.