Magento: Redirect functions

The redirect functions are present in Mage_Core_Controller_Varien_Action class.

/* Redirect to certain url  */
_redirectUrl($url)

/* Redirect to certain path */
_redirect($path, $arguments=array())

/* Redirect to success page */
_redirectSuccess($defaultUrl)

/* Redirect to error page */
_redirectError($defaultUrl)

/* Set referer url for redirect in response */
_redirectReferer($defaultUrl=null)

/*  Identify referer url via all accepted methods (HTTP_REFERER, regular or base64-encoded request param) */
_getRefererUrl()

/* Check url to be used as internal */
_isUrlInternal($url)

Example:

You can use the redirect functions in your controller class. Like below:-


$this->_redirect($path, $arguments=array());
// OR,
$this->_redirectUrl($url);

The other way is:-

For redirect URL:-


$url = "http://example.com";
Mage::app()->getFrontController()->getResponse()->setRedirect($url);

For redirect with path and arguments:-


Mage::app()->getFrontController()
       ->getResponse()
       ->setRedirect(Mage::getUrl($path, $arguments));

Redirect from Model or Observer

To redirect to any URL from Model or Observer class, you can use the following code:


$url = "http://example.com"; // e.g. Mage::getUrl('customer/account/login')
Mage::app()->getFrontController()
       ->getResponse()
       ->setRedirect($url);

Hope it helps. Thanks.