Magento 1.4.2 Multi-Website Admin Redirect Problem – A Quick Workaround
Intro
We have recently updated a Magento multi-website (multi-store) from 1.3.2 to 1.4.2 and ran into a strange bug when viewing any Order. At first we couldn't quite figure out why it was happening, but we soon realized it was a bug in the Magento Admin. We couldn't find any information on the web so far. So naturally we went looking for a solution ourselves. We set up a mutli-store Magento 1.4.2 and replicated the problem.
The Problem
In a multi-store environment, the orders are coming in from different websites. When you view the order in the Admin and click any action button in the top right corner (except "Back", of course), the action is executed, but you are being falsely redirected to a non-existing page for the store the order originated from.
Multi-Website Setup:
- Main website: www.storeA.com (this is where we access the admin from)
- Other website: www.storeB.com (there is no admin here. you are redirected to www.storeA.com)
The order is placed from: www.storeB.com
In the admin (www.storeA.com), clicking on the action buttons redirects to www.storeB.com/admin/admin/sales_order/view/order_id/[order_id] rather than www.storeA.com/admin/admin/sales_order/view/order_id/[order_id].
Tracking the Issue
At the moment we still don't know why this is happening in detail, but here's what we have discovered after several hours of debugging so far. After the action has been executed, you are being redirected back to the order view. This is done by this:
$this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
in /app/code/core/Mage/Adminhtml/controllers/Sale/OrderController.php. For example on line 139 in the emailAction function.
For our setup this redirects to www.storeB.com/admin/admin/sales_order/view/order_id/[order_id]. If you follow this down the rabbit hole it gets kind of confusing. But bare with me, it will hopefully make more sense in the end. The function $this->_redirect is found in the inherited class Mage_Adminhtml_Controller_Action, found in /app/code/core/Mage/Adminhtml/Controller/Action.php on line 395. The second line is where the redirect is actually set:
protected function _redirect($path, $arguments=array())
{
$this->_getSession()->setIsUrlNotice($this->getFlag('', self::FLAG_IS_URLS_CHECKED));
$this->getResponse()->setRedirect($this->getUrl($path, $arguments));
return $this;
}
$this->getUrl($path, $arguments) is what we need to look for. We find it a few lines down on line 415.
public function getUrl($route='', $params=array())
{
return Mage::helper('adminhtml')->getUrl($route, $params);
}
From here we need to go to the adminhtml helper found in /app/code/core/Mage/Adminhtml/Helper/Data.php. The function is found on line 80:
public static function getUrl($route='', $params=array())
{
return Mage::getModel('adminhtml/url')->getUrl($route, $params);
}
And from here we go to the model adminhtml/url, actually Mage_Adminhtml_Model_Url which is found in /app/code/core/Mage/Adminhtml/Model/Url.php. The getUrl function is found on line 70. On line 78 we get the URL from the parent, which is Mage_Core_Model_Url. We find that at /app/code/core/Mage/Core/Model/Url.php. This is the file that handles ALL Url requests. DO NOT, I repeat, DO NOT edit this file. If you do, you run the risk of rendering your entire store useless.
On line 805 we find the function getUrl. On line 836 we find:
$url = $this->getRouteUrl($routePath, $routeParams);
The function for it is found on line 604. And finally, line 622 returns us with the wrong URL.
$url = $this->getBaseUrl().$this->getRoutePath($routeParams);
This is because $this->getBaseUrl() returns the base URL (e.g. www.domain.com) for the store. Following that we go to line 306, and from there to line 326 where we find that it gets the base URL for the store.
return $this->getStore()->getBaseUrl($this->getType(), $this->getSecure());
The workaround shown below will make use of this function. The trick is found on line 308, or rather 309. More on that later, though. If we follow this further, we go to /app/code/core/Mage/Core/Model/Store.php. The function we are looking for is found on line 406. Here it gets the base_url from the store configuration. The problem is, that this returns us the URL for the storeB store, which is www.storeB.com. So, this is the cause of the redirect, but not the problem. The problem is that the wrong store is set to begin with.
The Workaround
We can use the following workaround to get around the redirect, but it certainly does NOT fix the issue. Use this at your own risk.
Copy /app/code/core/Mage/Adminhtml/Controller/Action.php to /app/code/local/Mage/Adminhtml/Controller/Action.php.
Remember: NEVER edit any files in /app/code/core. You can save your changes in /app/code/local. That way you can easily find your changes and if need be, replicate them when you upgrade to a newer Magento version.
Find the getUrl function on line 415 and add the following code:
/* workaround for multi-store redirect issue in 1.4.2 */
$params['_store'] = Mage::getModel('core/store')->load(0);
/* end workaround */
Your function should now look like this:
public function getUrl($route='', $params=array())
{
/* workaround for multi-store redirect issue in 1.4.2 */
$params['_store'] = Mage::getModel('core/store')->load(0);
/* end workaround */
return Mage::helper('adminhtml')->getUrl($route, $params);
}
That's it. Save the file, refresh your cache (both Magento's and your browser's cache, just to be sure) and give it a try.
The reason this works, is because we pass along the correct store for the Admin, which always has the ID 0 (Zero). Following our trail above we find that in /app/code/core/Mage/Adminhtml/Model/Url.php on line 309 the new store is set and the correct base URL is returned.
The REAL Fix
We are still looking for the real problem and hope to find a REAL fix for it. We are aware that the above workaround seems to solve the problem, but we want to make sure the problem is fixed at the source, rather than fixing the symptom. If you would like to help track this issue, you can help us by commenting on this post. If we find a good solution, we will let you know.



February 25th, 2011 - 08:43
Thanks, great workaround!
April 20th, 2011 - 10:12
I’ll have to give this a try. Thanks for your research into this problem!
April 26th, 2011 - 02:23
I am getting page not found error while I change the language in Mgento.May I know how to solve this issue ?