Magento – Product and category url rewrite concept

catalogcategoryurl-rewrite

I have a confusion in product and category url rewrite concept. Is magento stores the url in database? If yes which table? If no how we get the category and product url from collections? like this.

$product_collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id', 16)
    ->addUrlRewrite();
$product_collection_url = $product_collection->getFirstItem()->getProductUrl();

and what is addUrlRewrite() ?
Please explain.

Best Answer

These questions might help to explain it:

Without addUrlRewrite you may get the URLs in the form of http://www.my site.com/catalog/product/view/id/2309/s/shopcat/category/373/ which don't look very nice!

addUrlRewrite gives the URL in the form with the URL key, eg. http://www.site.com/product-url-key.html.

Adding a category ID eg. addUrlRewrite(15) gives the category URL for that product, eg. http://www.site.com/category-15/product-url-key.html, providing the product is in that category of course!

The latter will only work if in System/Configuration/Catalog/Search Engine Optimization you have Use Categories Path for Product URLs set to Yes and Use Canonical Link Meta Tag For Products set to No. See here for more on that http://www.magentocommerce.com/knowledge-base/entry/canonical-meta-tag

Hope that helps!