I have a link called "BACK TO.." which will redirect to the previous page.I have used something like the following..

<div class="link2Back">
    <a href="<?php echo Mage::getSingleton('core/session')->getLastUrl();?>">Back to ... </a>

But it is not working..It redirects to 404 not found page.


You could try to get the referer from the request and if it exists and is not an external URL, use it. If not, fall back to the base URL:

$url = Mage::app()->getRequest()->getServer('HTTP_REFERER');
if ((strpos($url, Mage::app()->getStore()->getBaseUrl()) !== 0)
    && (strpos($url, Mage::app()->getStore()->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, true)) !== 0)
) {
    $url = Mage::app()->getStore()->getBaseUrl();

This is a simplified version of the _getRefererUrl() method of the controller base class.

  • Hello @fschmengler. It worked fine. – Kishore Patra Dec 22 '15 at 10:54

Maybe try this:

<div class="back-button">
    <button onclick="history.back();" class="button" title="<?php echo $this->__('Back') ?>" type="button">
            <span><?php echo $this->__('Back') ?></span>

Here is another way: http://www.codexpedia.com/magento/getting-the-last-url-and-determine-if-its-a-home-page-or-any-other-pages-in-magento/

  • Hello @Reinsch . It is working fine this way. Thanks. But I want to know whether it is possible in magento syntax ? – Kishore Patra Dec 22 '15 at 4:52
  • Well this is what is used in magento too. – Reinsch Dec 22 '15 at 8:53

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.