Magento Redirect Functions

Posted by Damodar Bashyal on May 13, 2013

 

Magento core provides some built-in redirect functions to redirect url to new urls. Let's check few of them below:

From any of your Magento controller you can redirect user to any url as defined below. If you want to use redirect function in your magento observer model check this Technooze_T404 extension which I created to handle 404 issue in multi-store magento installation as defined here - stackoverflow question.

ref: protected function _redirectUrl($url)

// e.g. $url = "http://damodarbashyal.com/contact-us";
$this->_redirectUrl($url);
or,
Mage::app()->getFrontController()->getResponse()->setRedirect($url);

If you find magento is not redirecting to new url then call function sendResponse() after you set redirecting url.

Mage::app()->getFrontController()->getResponse()->setRedirect($url)->sendResponse();

Alternatively, you can pass url path instead of full url as:

ref: protected function _redirect($path, $arguments = array())

// $path = "contact-us";
// $arguments = array('param1' => 'value1')
// $url = Mage::getUrl($path, $arguments);
$this->_redirect($path, $arguments = array())
or,
Mage::app()->getFrontController()->getResponse()->setRedirect($url);

You can redirect to internal success page as:

ref: protected function _redirectSuccess($defaultUrl)

$this->_redirectSuccess($successUrl); 
// note: it should be internal url otherwise it will redirect to base url

Similarly you can redirect to internal error page as:

ref: protected function _redirectError($defaultUrl)

$this->_redirectError($errorUrl);
// Note: it must be internal url

Also, you can redirect to referrer url

ref: protected function _redirectReferer($defaultUrl)

$this->_redirectReferer($refererUrl);
// $refererUrl is only used if no referrer found, otherwise it will redirect to original referrer.

Source: app\code\core\Mage\Core\Controller\Varien\Action.php

For your reference, functions used above as appeared on source file:

    /**
     * Set redirect url into response
     *
     * @param   string $url
     * @return  Mage_Core_Controller_Varien_Action
     */
    protected function _redirectUrl($url)
    {
        $this->getResponse()->setRedirect($url);
        return $this;
    }

    /**
     * Set redirect into response
     *
     * @param   string $path
     * @param   array $arguments
     * @return  Mage_Core_Controller_Varien_Action
     */
    protected function _redirect($path, $arguments = array())
    {
        return $this->setRedirectWithCookieCheck($path, $arguments);
    }


    /**
     * Redirect to success page
     *
     * @param string $defaultUrl
     * @return Mage_Core_Controller_Varien_Action
     */
    protected function _redirectSuccess($defaultUrl)
    {
        $successUrl = $this->getRequest()->getParam(self::PARAM_NAME_SUCCESS_URL);
        if (empty($successUrl)) {
            $successUrl = $defaultUrl;
        }
        if (!$this->_isUrlInternal($successUrl)) {
            $successUrl = Mage::app()->getStore()->getBaseUrl();
        }
        $this->getResponse()->setRedirect($successUrl);
        return $this;
    }

    /**
     * Redirect to error page
     *
     * @param string $defaultUrl
     * @return  Mage_Core_Controller_Varien_Action
     */
    protected function _redirectError($defaultUrl)
    {
        $errorUrl = $this->getRequest()->getParam(self::PARAM_NAME_ERROR_URL);
        if (empty($errorUrl)) {
            $errorUrl = $defaultUrl;
        }
        if (!$this->_isUrlInternal($errorUrl)) {
            $errorUrl = Mage::app()->getStore()->getBaseUrl();
        }
        $this->getResponse()->setRedirect($errorUrl);
        return $this;
    }

    /**
     * Set referer url for redirect in response
     *
     * @param   string $defaultUrl
     * @return  Mage_Core_Controller_Varien_Action
     */
    protected function _redirectReferer($defaultUrl=null)
    {

        $refererUrl = $this->_getRefererUrl();
        if (empty($refererUrl)) {
            $refererUrl = empty($defaultUrl) ? Mage::getBaseUrl() : $defaultUrl;
        }

        $this->getResponse()->setRedirect($refererUrl);
        return $this;
    }

Like us on facebook to follow dltr.org updates.

Amrin posted on - Thursday 24th of March 2016 12:50:20 AM

Sir how do i redirect to a cms page froma phtml file
 
not published on website


QR Code: Magento Redirect Functions