0

I would like to add a static path to product URLs. Something like 'product/regular-product-path'. I would like this to be a rule, not something I have to add manually in Catalog >> URL Rewrite Manager.

I have found this post http://www.placementedge.com/blog/how-to-add-a-prefix-to-magento-product-urls/ where a solution is suggested. It apparently works, but it is probably targeted towards an earlier version of Magento because the script the writer suggests to edit doesn't exist in my 1.8.1 version.

He suggests editing /app/code/local/Mage/Catalog/Model/Url.php, but the close I could find was /app/code/local/Mage/Catalog/Model/Product/Url.php. If /app/code/local/Mage/Catalog/Model/Product/Url.php is the file I should be editing, which lines should I change to add my static URL prefix? Thank you.

  • .it hope please check this link ,i throught i will be better magento.stackexchange.com/questions/24869/… – Amit Bera Jun 26 '14 at 9:57
  • Thank you for the help, I am gonna check this link, I will be resuming working on this issue this week. I will let you know if this helps. – Marcos Buarque Jun 29 '14 at 1:57
  • Why on earth do people fiddle with the Magento internals, you just add a one or two keyword prefix to the url and switch off add category path to product urls, we happily rank first page with those. – user2935 Jul 26 '14 at 18:20
  • Serpyre, thank you for your comment. Can you please be more clear when you say "you just add a one or two keyword prefix to the url". That is exactly my point, what I am trying to achieve. Do you mean for each product (let's say, a book) you add something like "book-literature-[book-name]"? If you are selling Shakespeare's Hamlet, you would have "www.mybookstore.com/book-literature-hamlet". Is this what you mean? Do you simply edit the URL Key to add 'book-literature' to the beginning of the URL? I was more thinking of an automated process of adding "book-literature/" to each product prefix. – Marcos Buarque Jul 30 '14 at 13:02
  • You have it exactly, always keep it simple with Magento, and remove the add category path to urls - you don't have to use canonicals which are unreliable. You can easily write a script to do it. Best to switch off create permanent redirect unless your store is already indexed, then leave it on. You would also be better off with www.mybookstore.com/book-literature-william-shakespeare-hamlet/ – user2935 Jul 30 '14 at 14:48
1

You need to copy /app/code/core/mage/catalog/model/url.php file to /app/code/local/mage/catalog/model/url.php and at lines 779-787 replace the following code

if ($category->getLevel() > 1) {
   // To ensure, that category has path either from attribute or generated now
   $this->_addCategoryUrlPath($category);
   $categoryUrl = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(),
    false, $storeId);
   $requestPath = $categoryUrl . '/' . $urlKey;
} else {
   $requestPath = $urlKey;
}

with

if ($category->getLevel() > 1) {
 // To ensure, that category has path either from attribute or generated now
 $this->_addCategoryUrlPath($category);
 $categoryUrl = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(),
  false, $storeId);
 $requestPath = 'product-prefix/' .$categoryUrl . '/' . $urlKey;;
} else {
 $requestPath = 'product-prefix/' .$urlKey;
}
|improve this answer|||||
  • Thank you for the help, I am gonna go over your answer carefully, I will be resuming working on this issue this week. I will let you know if this helps. – Marcos Buarque Jun 29 '14 at 1:57
  • 1
    Do not ever copy anything to the local code pool to make a change. Create a proper rewrite of the class method. – philwinkle Jul 26 '14 at 18:21
  • Indeed, NEVER do that, except in ponctual situations, where you want to test some result and don't want do bother overriding the whole stuff the proper way, but you'll have to delete all your changes, so it might not be your best option. Try and read Inchoo on that matter. – Julien Lachal Jul 30 '14 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.