Rewrite

In Magento, errors can occur when using the language selector to navigate between different pages for which different URL keys have been specified, e.g. "about-us" for the English "About Us" page and "ueber-uns" for the German equivalent.

So, if you are on the "about-us" page and want to navigate to the German translation, Magento searches for the German version of "about-us", which does not exist in this example.

Why does Magento search for the German version of the English URL key?

To find the answer, we need to delve a little deeper into the Magento structure. First, let's take a look at the Mage_Core_Model_Url_Rewrite_Request class, which can be found in the file \app\code\core\Mage\Core\Model\Url\Rewrite\Request.php. Within this class, we are particularly interested in the _rewriteDb() function. In this function, the following essential query for us

if (!empty($stores[$fromStore])) {

is answered with "no" on every iteration, meaning that the array key "$fromStore" contains no value — more precisely, the array key does not exist in this case.

And why? If we output the "$fromStore" variable, it contains the language code (e.g. "en" in the English store view). However, this is already where the error occurs. The "$stores" array does contain all stores, but only indexed by the store ID (as the key). This means that we need to look a little further up to see how the "$fromStore" variable is populated. Here we find:

$fromStore = $this->_request->getQuery('___from_store');

Since we need the store ID, we simply change the above statement to:

$fromStore = Mage::getModel('core/store')->load($this->_request->getQuery('___from_store'), 'code')->getId();

This allows us to pass through the if statement shown above without any problems, and the rewrite and language selector then work perfectly.

To avoid making changes to the Magento core and keep the Magento installation update-compatible, we naturally do not modify the file \app\code\core\Mage\Core\Model\Url\Rewrite\Request.php. Instead, we create an override, for example, using the file \app\code\local\Mage\Core\Model\Url\Rewrite\Request.php.

Tested with Magento CE 1.8.0.0. ... back to the blog