How can we get the second last url in Magento 1.9?

I currently use this: <?php echo Mage::getSingleton('core/session')->getLastUrl();?> To get the last URL.

But I want to get the second last URL after adding a product to the cart. Because the code above give in that case the url to add the product to the cart.

How can I get the url before that, so the second last?


You can use


to get the second last URL or Referrer URL. Or if you need it as a string


$_SERVER['HTTP_REFERER'] would output the url or you may use

$previousurl = Mage::helper('core/http')->getHttpReferer() ? Mage::helper('core/http')->getHttpReferer()  : Mage::getUrl();
| improve this answer | |
  • Thanks! But that does already set a redirect. I need only the URL. – JGeer Dec 27 '18 at 11:42
  • Thanks again! I thought it worked well, but it seems that the previous url is wrong. It looks like is is being cached into Cloudflare. Any idea? – JGeer Dec 27 '18 at 15:17
  • You can try deleting Cloudflare cache support.cloudflare.com/hc/en-us/articles/… followed by flushing Magento cache storage. – Kumar A. Dec 28 '18 at 7:39

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.