11

As you may know, if you have products that share a url key, the url key will have a digit appended to it:

i.e

http://www.example.com/main-category/sub-category/product-name-**6260**.html

How do I find the source of that 6260 (which is one of the #'s appended to my urls)? I tried product id, sku, I cannot find the source of it. The reason I ask is because if I can find it, I can create a string replace function to flush it out of url's before I echo them on certain product listing pages.

Thanks.

Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
Joel
  • 183
  • 1
  • 2
  • 8
  • Have you looked in the URL Rewrites in the admin area? From [this](http://stackoverflow.com/questions/5019584/magento-puts-1-in-my-urls-can-i-remove-programatically) post on SO, and from what I have seen myself, it's something to do with the URL Rewrites and an indexing issue – CCBlackburn Nov 28 '12 at 21:40
  • My concern is I am taking control of a site with *thousands* of configurable products (which is what causes the url key duplicates) and nobody took care of this before. I am not sure if there is a section in the admin that solves this in one swoop. My concern is that, unlike that post you refer to, my url's with digits are dynamic and there is no real pattern I can find. That's why I wonder if there is a dynamic way to find out what the source of the dynamically appended digit comes from. That is my concern. There is obviously a function that controls it... Thank you. – Joel Nov 28 '12 at 21:52
  • Just had a thought, is the URL key for the configurable product different from the simple product? If not, this will be causing issues too – CCBlackburn Nov 28 '12 at 22:00
  • That's my hell, correct. The simple and grouped products share that url key and there are thousands of these types of products and lord knows how many times they share a url. Most products, when I use a function to pull a product category url, work fine. But a few of them sneak attack everything. So I was wondering what function would control this or if there is a way to say "append an sku, not this mystery number". I guess the point is, it's based on the # of url key issues, not something related to the product itself? For instance, if there are 2500 duplicates, one url key will have 2500? – Joel Nov 28 '12 at 22:18

3 Answers3

22

Before we get to the location in code where this happens, be advised you're entering a world of pain.

  1. There's no simple rule as to how those numbers are generated. There's cases where it's the store ID, there's cases where it's the simple product ID. There's cases where it's neither

  2. Even if there was, it's common for not-from-scratch Magento sites to contain custom functionality that changes this

  3. Ultimately, since Magento's human readable/SEO-friendly URLs are located in the core_url_rewrite table, it's possible for people to insert arbitrary text

Warnings of doom aside, the Model you're looking for is Mage::getSingleton('catalog/url'). This contains most of the logic for generating Magento Catalog and product rewrites. All of these methods end by passing the request path through the getUnusedPath method.

#File: app/code/core/Mage/Catalog/Model/Url.php
public function getUnusedPath($storeId, $requestPath, $idPath)
{
    //...
}

This method contains the logic for for creating a unique number on the end of the URL. Tracing this in its entirely is beyond the scope of a Stack Overflow post, but this line in particular is enlightening/disheartening.

$lastRequestPath = $this->getResource()
    ->getLastUsedRewriteRequestIncrement($match[1], $match[4], $storeId);
if ($lastRequestPath) {
    $match[3] = $lastRequestPath;
}
return $match[1]
    . (isset($match[3]) ? ($match[3]+1) : '1')
    . $match[4];

In particular, these two lines

$match[3] = $lastRequestPath;
//...
. (isset($match[3]) ? ($match[3]+1) : '1')
//...

In case it's not obvious, there are cases where Magento will automatically append a 1 to a URL, and then continue to increment it. This makes the generation of those URLs dependent on system state when they were generated — there's no simple rule.

Other lines of interest in this file are

if (strpos($idPath, 'product') !== false) {
    $suffix = $this->getProductUrlSuffix($storeId);
} else {
    $suffix = $this->getCategoryUrlSuffix($storeId);
}    

This $suffix will be used on the end of the URL as well, so those methods are worth investigating.

If all you're trying to do is remove numbers from the URL, you might be better off with a regular expression or some explode/implode string jiggering.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • 1
    It's worth noting that there appears to be a bug in 1.7 that causes Magento rolls always generate a new value for any product that has a duplicate name. Regardless of whether anything has changed since the last reindex ran. – Peter O'Callaghan Nov 29 '12 at 08:02
  • Sorry, hit post by accident. Hard to type on phone whilst on bus. Update with more detail. – Peter O'Callaghan Nov 29 '12 at 08:07
  • 1
    Alan, you are the man who taught me what I am learning in Magento, so a personal thank you. Cags, great heads up re: 1.7. I will look into that and await the update you mentioned. I'm thinking that Alan is right, the only real solution is to destroy the 3-4 digits appended to the url and just print out a 'clean' url so it guarantees that it will load instead of hitting the 404 error. Thank you. – Joel Nov 29 '12 at 15:09
  • How can we get the `url_key` which returns the url with digit in `catalog_product.list` SOAP API? I currently get url without the number appended(`chelsea-tee`) and which remains common among many products. I need unique URL(`chelsea-tee-720`) to the product using which I can directly navigate to the product. @AlanStorm I have exposed custom API wrapping `catalog_product.list` but have no idea how to get the reachable URL. – Amit Patel Nov 14 '17 at 07:19
2

I have little to no idea why this works but this worked for me. Most probably because it makes urls non-unique. Magento ver. 1.7.0.2 had suddenly started adding numbers as suffixes to my new products' names, even if their url keys and names were different from the old products. On a hunch, I went to System -> Configuration -> Catalog -> Search Engine Optimizations -> Product URL Suffix and changed the default .html to -prod.html. I guess you could change it to any suffix you wanted to. Then I re-indexed my website, refreshed cache, and presto! All the numbers were gone from the product urls. The product urls now all have the format custom-product-name-prod.html. The canonical tag also shows custom-product-name-prod.html so I'm double happy.

Don't know if it'll work for others, but I hope it does. Do note that I did have old and new products with duplicate URLs and that I had disabled old products before doing this procedure. So if you have 2 products with the same url key and both are enabled, then this may NOT work for you.

PHP Bugs
  • 1,133
  • 12
  • 23
Agrim
  • 471
  • 9
  • 19
0

There's a problem! As you look in the edit product you can see the link is correct but on your store the url is different.

  • Step 1: Edit the URL and add SS to the end of the custom url. Press Save. \product-namess.html
  • Step 2: Go to Catalog -> URL Rewrite
  • Step 3: Narrow Down the Criteria to Include Only The Problem Situation. Press Search.
  • Step 4: Download Jib Bit Mouse Recorder / Macro Recorder. Its Free I pay.
  • Step 5: Edit the First Rewrite. Press Delete and OK.
  • Step 6: Press Record on JitBit. Edit the 1st Rewrite. Delete. OK. Press Stop.
  • Step 7: Drop Down Next to Play Select X Times. Write the Number of Records. Press OK.
  • Step 8: Watch the Program Delete All the Records for You.
  • Step 9: Catalog -> Manage Products. Remove the SS from the end of the custom URL. Do not check create redirect. Fixed.

Mine was 279 records of changing that product. So it took about an hour.

Sparxx
  • 1
  • 4
  • Safety is using a mouse recorder. You can only click so many times with your fingers in your lifetime. – Sparxx Sep 30 '18 at 11:59