The question:
By default URL Key
on product page is Global scoped.
EDIT:
As suggested by FlorinelChis the scope can be changes in Manage attributes.
However this breaks the store view switcher behavior.
This is has been tested on 1.7.0.2 with samples data and “Add store code to URL” enabled:
- edit a product and set a different url for a particular storeview (french)
- Re-index
- Open product page on the site on english store view
- Switch to french: you will have the page URL containg
/French/
-
Switch back to english -> 404 page error ( the url miss the store code
/default/
how to make it work correctly with store view/language switch ?
Details:
- URL for english :
/default/sony-vaio-vgn-txn27n-b-11-1-notebook-pc.html
- URL for french :
/french/sony-vaio-vgn-txn27n-b-11-1-notebook-pc-french.html
If I’m on english site on this page -> /default/sony-vaio-vgn-txn27n-b-11-1-notebook-pc.html
Then I switch to french:
I got this URL (the store code is missed):
MAGEDOMAIN/sony-vaio-vgn-txn27n-b-11-1-notebook-pc-french.html
So magento re-write the url correctly but miss the store code for some reason
Reference:
For sure this is related to /core/model/store.php
and /core/model/url/rewrite.php
, and in particular to those methods:
Mage_Core_Model_Url_Rewrite::rewrite
Mage_Core_Model_Store::getCurrentUrl
UPDATE
If you are on 1.9.1 @Vinai fix will not work, check the new answer I have added
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
The problem is a bug in the model Mage_Core_Model_Url_Rewrite_Request
(Magento 1.8) and Mage_Core_Model_Url_Rewrite
(earlier versions).
The section of core code in 1.8 looks like this:
// Section from Mage_Core_Model_Url_Rewrite_Request::_rewriteDb()
$fromStore = $this->_request->getQuery('___from_store');
if (!$this->_rewrite->getId() && $fromStore) {
$stores = $this->_app->getStores();
if (!empty($stores[$fromStore])) {
$store = $stores[$fromStore];
$fromStoreId = $store->getId();
} else {
return false;
}
The Bug: the value of the query parameter is the store code, (in my case de
, en
or fr
).
The keys of the array returned by app->getStores()
are the numeric store IDs.
Thats why if (!empty($stores[$fromStore])) {
always fails.
Once that bug is fixed, another bug becomes apparent later in the same method (I think only in 1.8):
$targetUrl = $this->_request->getBaseUrl() . '/' . $this->_rewrite->getRequestPath();
The request objects base url always is the Magento base_url, without the store code.
Using $currentStore->getBaseUrl()
instead there fixes that bug, too.
Once those two issues are fixed the language switcher works fine. Here is an extension which does exactly that for Magento 1.8 (CE): https://github.com/Vinai/VinaiKopp_StoreUrlRewrites
In Magento 1.7 the issue might be something different. I still thought I’d add this answer, just in case google brings somebody else here who is running 1.8 or newer.
Method 2
Actually I found a workaround for this issue on Magento 1.7.0.2 if you are running Magento 1.8 looks to Vinai’s detailed explanation:
It looks that part of the problem is related to the request controller Mage_Core_Controller_Request_Http
.
If you look at line 161 there is this condition:
elseif ($storeCode !== '') {
$this->setActionName('noRoute');
}
Commenting it out fix the 404 error when I switch to a different Store in a category/product pages.
However for some unknown reason some time the store code is missed in the response Url but this is not causing issue anymore as both url works now:
- MAGEDOMAIN/sony-vaio-vgn-txn27n-b-11-1-notebook-pc-french.html
- MAGEDOMAIN/sony-vaio-vgn-txn27n-b-11-1-notebook-pc.html
It is stil not clear to me if the comment of this condition can cause other issue
Method 3
Some updated info for Magento 1.9.1
The bug @Vinai pointed out looks solved in this version anyway for other reason the functionality is still broken ( for configurable products )
The problem real problem is probably here Mage_Catalog_Model_Resource_Url
however I don’t have time and I dont’ want to touch a such delicate part of the core.
Explanation for a workaround:
The entry point is always this classMage_Core_Model_Url_Rewrite_Request
and in particular the method _rewriteDb()
How _rewriteDb()
works:
- First it try to load the request for the current store
(139): $this->_rewrite->loadByRequestPath($requestCases);
- then if I cannot find it (no id) and has a
___from_store
parameter
(142): if (!$this->_rewrite->getId() && $fromStore) {
- try to load a rewrite for the
___from_store
:
(152): $this->_rewrite->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
- if it find it, it use the
id_path
to load the one for the current store:
(159): $this->_rewrite->setStoreId($currentStore->getId())->loadByIdPath($this->_rewrite->getIdPath());
Everything looks fine however there is an issue in the url_rewrite data and so with the index functionality ( at least for configurable products ):
- even if we are switching store and the new store has different url a rewrite at line 139 is loaded.
The problem is that this rewrite points to the wrong id_path
( instead of pointing to the configurable product id it is pointing to one of its simple product id )
Now a workaround is to remove the !$this->_rewrite->getId()
condition and so magento try to find a redirect always when there a $fromstore
parameter
- The best would be to fix the
catalog_url
index and remove the wrong
rewrite it creates.
Here the code for the fast workaround ( you will need to create a module and rewrite Mage_Core_Model_Url_Rewrite_Request
class by your self):
protected function _rewriteDb()
{
if (null === $this->_rewrite->getStoreId() || false === $this->_rewrite->getStoreId()) {
$this->_rewrite->setStoreId($this->_app->getStore()->getId());
}
$requestCases = $this->_getRequestCases();
$fromStore = $this->_request->getQuery('___from_store');
if ($fromStore) {
$stores = $this->_app->getStores(false, true);
if (!empty($stores[$fromStore])) {
/** @var $store Mage_Core_Model_Store */
$store = $stores[$fromStore];
$fromStoreId = $store->getId();
} else {
return parent::_rewriteDb();
}
$this->_rewrite->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
if (!$this->_rewrite->getId()) {
return parent::_rewriteDb();
}
// Load rewrite by id_path
$currentStore = $this->_app->getStore();
$this->_rewrite->setStoreId($currentStore->getId())->loadByIdPath($this->_rewrite->getIdPath());
$this->_setStoreCodeCookie($currentStore->getCode());
$targetUrl = $currentStore->getBaseUrl() . $this->_rewrite->getRequestPath();
$this->_sendRedirectHeaders($targetUrl, true);
}
if (!$this->_rewrite->getId()) {
return parent::_rewriteDb();
}
$this->_request->setAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$this->_rewrite->getRequestPath());
$this->_processRedirectOptions();
return true;
}
Method 4
URL Key is an attribute. You can edit it from: Catalog -> Attributes -> Manage Attributes.
Search for url_key and click on it.
Change the Scope and Save.
Now you can have different URL keys for products on each store view.
Method 5
So you want to change the URL for each store view?
At present, you modified the product URL at the score scope for your French store to be different to your English store? And when you switch between the two, you get a 404. This would be expected behaviour.
Magento won’t store different URL rewrites for other store views. So when you hit /french/product1
on the French store, the URL will match in the table and it will load. But when you hit it in the English store, there will be no match and thus will 404.
What it sounds like you need is to just “Add store codes to URL” – which will leave your URL keys alone, but prefix all respective URLs with your store code. This should then allow your store switcher to function.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0