Magento – How to get URL rewrite Target Path programmatically

indexmagento2modelurl-rewrite

I need to get programmatically the "Target Path" field that I have set in the Magento 1.8 Admin Panel, using Catalog -> URL rewrite management. How do I get it?

Best Answer

The responsible model is Mage_Core_Model_Url_Rewrite which has several possible methods to load:

  • $rewrite = Mage::getModel('core/url_rewrite')->loadByRequestPath($path)
  • $rewrite = Mage::getModel('core/url_rewrite')->loadByIdPath($path) and the usual
  • $rewrite = Mage::getModel('core/url_rewrite')->load($id)

And then it's a standard attribute getter:

$rewrite->getTargetPath()