Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Mage::getUrl doesn’t search rewrites
 
Paul Bunkham
Jr. Member
 
Total Posts:  3
Joined:  2010-07-20
 

Hi,

I’m sure I’m just missing something, but I’ve got rewrites turned on for categories and I’m not getting the expected result when I call Mage::getUrl(’catalog/category/view’,array(’id’=>4,’_use_rewrite’=>true))

In the rewrite rules I have a rule that has the request path as ‘jewellery/’ and the target path as ‘catalog/category/view/id/4’. Looking at the documentation for getUrl (http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/geturl_function_parameters) it says that setting _use_rewrite to true should cause the module/controller/action/parameters to be looked up in the database, but the above call just returns the url http://magento.local/catalog/category/view/id/4 instead of http://magento.local/jewellery/ as expected.

The rewrite rule is working as going to http://magento.local/jewellery/ displays the category as expected, and I think the problem might be that this database lookup never actually happens. Looking at the code I’ve tracked down the rewrite check to the getRoutePath method in Mage_Core_Model_Url. This checks if there’s a rewrite equivalent for the current request url and if so, uses that path, but it doesn’t check it for the path passed in.

I know you could get the category url in other ways, but this would be a problem for any rewrite. I might have a custom module that sets up its own rewrite rules and calling getUrl with the correct parameters should return the SEO friendly equivalent, but I don’t see how that would be the case.

Has anyone got any experience with this? I’ve searched around but can’t see anyone else having this problem, although that could be because I’m the only one doing it wrong!

Any help would be really appreciated.

Cheers,

Paul.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Tom0
Jr. Member
 
Total Posts:  7
Joined:  2011-11-01
 

Ive just had I think exactly the same issue. As far as I can see it looks as though the rewrites only work one-way? Ie, if you create a rewrite ruule and enter the new url into your browser’s address bar it works and goes to the right place. But using Mage::getUrl(’mymodule’, array(’_use_rewrite’ => true)); to generate the rewritten url doesnt seem to lookup the rewrites.

To get around this I created my own function in a helper to get the rewritten url for my module… its a bit rubbish but:

$rewrite Mage::getModel('core/url_rewrite');
            if (
Mage::app()->getStore()->getId()) {
                $rewrite
->setStoreId(Mage::app()->getStore()->getId());
            
}
            
            $idPath 
'mymodule';
            
            
// Look for a rewrite rule with this id
            
$rewrite->loadByIdPath($idPath);

            
// If found return our URL rewrite
            
if ($rewrite->getId()) {
                
return Mage::getUrl($rewrite->getRequestPath());
            
}
            
            
// Else return standard URL
            
return $idPath;

Is this (as I would hope) totally unnecessary? Is this a bug?

Tom

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mondane
Jr. Member
 
Total Posts:  26
Joined:  2011-09-19
 

I placed a bug report: http://www.magentocommerce.com/bug-tracking/issue?issue=12741

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top