6

I am in catalog\product\compare\list.phtml and I want to redirect to the homepage, like this:

if($someThing) {
    // redirect to homepage
    $this->_redirect('home'); // --> doesnt work
    $this->_redirect(Mage::getBaseUrl()); // --> doesnt work
}

I tried several things, none worked so far. How do I do it right?

Thanks!

2
  • 2
    try $this->_redirect('/'); – Keyul Shah May 14 '14 at 8:35
  • keyul It Worked for me thanx a lot – PRashant PUrohit Apr 7 '16 at 13:36
11

From a template you can simply use the following:

Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl());

I am interested as to why you are doing redirects directly in the template rather than at the controller level?

1
  • Agree. JavaScript might be another option, closer to a "view" solution. Still you might want to move this to a controller. – mbalparda Jul 15 '14 at 23:07
9

As _redirect() is a controller class function .By using this you cannot direction at any pages from phtml page. So you need to controller object which will redirect to home page

Mage::app()->getFrontController()->getResponse()
                ->setRedirect(Mage::getBaseUrl())
                ->sendResponse();
            exit;

exit stop execution of current page

0
7

You should never redirect to the homepage from a template.
Do that from the controller before any output is done.
You can use the code that DavidManners gave you, but do it before you call renderLayout in the controller.

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.