11

In my module, after execution of my script, I need to make a redirect to a page with a query string in the URL.

Here is what I have:

$redirectUrl = 'http://magento.local/en_en/shop/index';
$redirectArgs = array('test' => '1');
$this->_redirect($redirectUrl, $redirectArgs);

I also tried:

Mage::app()->getFrontController()->getResponse()->setRedirect($redirectUrl, $redirectArgs)->sendResponse();

Both methods throw an error: There has been an error processing your request

What I expect is to be redirected to http://magento.local/en_en/shop/index?test=1

Does anyone know how I can achieve that?

Edit:

As suggested, I have tried:

$redirectUrl = 'http://magento.local/en_en/shop/index?test=1';
Mage::app()->getResponse()->setRedirect($redirectUrl);

No error, but nothing happens. I am not in a controller.

Edit 2:

I ended up using:

$redirectUrl = 'http://magento.local/en_en/shop/index?test=1';
Mage::app()->getResponse()->setRedirect($redirectUrl)->sendResponse();

This works as expected! Thank you.

4 Answers 4

10

Why not just build the url like this?

 $redirectUrl = 'http://magento.local/en_en/shop/index?test=1';

the second parameter of setRedirect is for the redirect code (301, 302).

If you want to build the url internaly you can try this:

$redirectUrl = Mage::getUrl('module/controller/action', array('_query'=>'test=1'));
4
  • And then? $this->_redirect($redirectUrl); ?
    – MrUpsidown
    Commented Jul 23, 2014 at 9:05
  • @MrUpsidown. _redirect if you are in a controller. If you are somewhere else: Mage::app()->getResponse()->setRedirect($redirectUrl);
    – Marius
    Commented Jul 23, 2014 at 9:06
  • Nevermind. Adding ->sendResponse() at the end did the job!
    – MrUpsidown
    Commented Jul 23, 2014 at 9:17
  • @MrUpsidown. Sorry. I forgot about sendResponse
    – Marius
    Commented Jul 23, 2014 at 9:19
2

The better way to do this is like this.

Mage_Core_Controller_Varien_Action::_redirect('urlpost/index/response', array('_secure'=>true,'_query'=>'string1=417'));
0

If you want to redirect to another URL with query parameters, you can do that like this:

$redirectUrl = 'http://magento.local/en_en/shop/index';
$query_parameters = array(
                '_query'=> array(
                    'test' => '1',
                    'test'=>'2'
                )
            );

$this->_redirect($redirectUrl, $query_parameters);

This will redirect you to: http://magento.local/en_en/shop/index?test=1&test=2

0

If you ended up here from google, are using a controller and want to redirect to another preserving the arguments you can use:

$this->_redirect('module/controller/action', $this->getRequest()->getParams());

Where module, controller and action can be replaced by * to preserve it's value. Another action in same controller:

$this->_redirect('*/*/anotherAction', $this->getRequest()->getParams());

Same action name, sibling controller:

$this->_redirect('*/sibling/*', $this->getRequest()->getParams());

And so on...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.