3

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?

7

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()
  • Thank you friend. Can you let me figure out how to set up the $path variable? – Walter Zoccarato Nov 14 '14 at 23:49
  • I have tried that: $path = $category->getRequestPath(); $rewrite = Mage::getModel('core/url_rewrite')->loadByRequestPath($path); $tmp = $rewrite->getTargetPath(); $path = $category->getPathId(); $rewrite = Mage::getModel('core/url_rewrite')->loadByIdPath($path); $tmp = $rewrite->getTargetPath(); But I can't get the right value. – Walter Zoccarato Nov 14 '14 at 23:58
  • What exactly are you trying to achieve? – Fabian Schmengler Nov 15 '14 at 1:33
  • Thank you. The code you suggest works fine. In order to find the redirection target path URL I have found that you have to call the functions in the following sequence: $path = $category->getRequestPath(); $rewrite = Mage::getModel('core/url_rewrite')->loadByIdPath($path); $targeturl = $rewrite->getTargetPath(); Because, wath is the request_path before the redirection, become the id_path in the final url_revrite. – Walter Zoccarato Nov 15 '14 at 11:25
  • Glad, I could point you in the right direction. In your question you didn't mention that you wanted to find the target path for a given category, so I could not tell you which method is the one you need. Don't forget to accept the answer if it solved your problem. – Fabian Schmengler Nov 15 '14 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.