My magento store has 3 languages. If someone links me a product from the "English" store view for example, and I'm on the "Spanish" store view, that product returns a 404 error.
So far in my investigation I've found this blog, but I'm currently trying to understand where that code goes. I understand it's in the file /app/code/core/Mage/Core/Model/Url/Rewrite.php
but I can't figure out where exactly in that file I should add that code snippet.
And I ain't even certain that will solve my problem.
Edit:
Ok, I've found this link: http://freegento.com/doc/db/d5d/_url_2_rewrite_8php-source.html
According to this, my file should have something similar to what I saw in the blog above, unfortunately, the function loadByRequestPath
on my file is different and it goes like this:
/** * Load rewrite information for request * If $path is array - we must load possible records and choose one matching earlier record in array * * @param mixed $path * @return Mage_Core_Model_Url_Rewrite */ public function loadByRequestPath($path) { $this->setId(null); $this->_getResource()->loadByRequestPath($this, $path); $this->_afterLoad(); $this->setOrigData(); $this->_hasDataChanges = false; return $this; }
Ok, that was quick! I solved my problem by replacing the loadByRequestPath
located in the file /app/code/core/Mage/Core/Model/Url/Rewrite.php
to this:
public function loadByRequestPath($path)
{
$this->setId(null);
if (is_array($path)) {
foreach ($path as $pathInfo) {
$this->load($pathInfo, 'request_path');
if (!$this->getId() && !isset($_GET['___from_store'])) {
$db = Mage::getSingleton('core/resource')->getConnection('default_read');
$result = $db->query('select store_id from core_url_rewrite WHERE request_path = "' . $pathInfo . '"');
if ($result) {
$storeIds = array();
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
$storeId = $row['store_id'];
$storeCode = Mage::app()->getStore($storeId)->getCode();
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://" . $_SERVER['HTTP_HOST'] . "/" . $pathInfo . "?___store=" . $storeCode);
exit();
}
}
}
}
}
else {
$this->load($path, 'request_path');
}
return $this;
}
Don't forget to make a local copy if you don't want any problems when upgrading Magento.