Home » Magento2 December 2009

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:-

The other way is:-

For redirect URL:-

For redirect with path and arguments:-

Redirect from Model or Observer

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

Hope it helps. Thanks.

Magento

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • sabnam

    can u provide some examples…

  • Please tell us how to call these? Do we have to instantiate the class? I tried calling it as an abstract method like this:
    Mage_Core_Controller_Varien_Action::_redirect( ‘my/location’ );
    and I get the error:
    Fatal error: Call to protected method Mage_Core_Controller_Varien_Action::_redirect() from context ‘Mage_Core_Block_Template’ ….

    so no luck so far… I’m still working on this, but any help is appreciated!
    Website developer Midlands

  • I try to instantiate it this time like this:

    $actor = new Mage_Core_Controller_Varien_Action ();
    $actor->_redirect( ‘my/location’ );

    but got this error:
    Fatal error: Cannot instantiate abstract class Mage_Core_Controller_Varien_Action in ….

    Shooting in the dark :-( Please advise!

    Website developer Midlands

  • Of course you can’t use the redirect functions like that since Mage_Core_Controller_Varien_Action works as singleton.

    Those are available on any Controller action and they are Zend methods.

  • Kat

    Thanks Mukesh for this, however, I also need an example.

    And the example code is:
    Mage::app()->getFrontController()
    ->getResponse()
    ->setRedirect('http://your-url.com');

    Thanks again!
    jazkat

  • this article is now updated with some examples..

  • 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));

  • Thanks jazkat.. I have updated my post as well by adding some examples..

  • Anupama3sept

    Hi,

    I am on Model/Type/Onepage.php and trying to redirect another page.
    I try:
    $url = “http://www.example.com”;
    Mage::app()->getFrontController()->getResponse()->setRedirect($url);

    but seems not working.. I also try
    $this->_redirectUrl($url);
    but No… even no header Location works.. :(

    is there any other way that we can do redirect from inside Model page to any other page??

  • @Anupama:

    Try doing this:

    $url = “http://www.example.com”;
    Mage::app()->getResponse()->setRedirect($url);

    i.e. without calling the FrontController.

    Hope this helps.

  • Wdecamp

    After a login, I would like my customers to be redirected to the page they were originally referred to. How can I do this?

  • Pingback: Add to cart in magento « Venkatraja's Blog()

  • I think Is not good Idea to directly access classes. 

    Still use the Mage Class in your Model or Block to access method(s).

    Mage Class has lot of of static method mean to be sharable around the process.

  • Pingback: Magento: Redirect i controllern | Erik Landvall()

  • Nabil

    You awasome dude !!!
    thnx a lot

  • Yogendra M

    This also work on magento ee too :) good finding

  • Teju

    i have problem to redirect site to anther site in magento
    How to fix this ?

  • Teju

    I have to solve this Problem…

  • Ravinder Singh

    Thanks, it really helped me….:)