0

First of all, did quite some research, but could not find the best solution in this case.

For SEO reasons, we need to add a fixed prefix (with a slash) to all product URLs.

Product URLs now look like the following:

shopdomain.com/product-name.html 

Expected result is to me URLs look like this:

shopdomain.com/car/product-name.html

Is there, by any chance, a standard Magento way for doing this? If not, what would be a possible workaround for this situation?

Any help or guidance is much appreciated.

  • car is your category ?? – Murtuza Zabuawala Dec 12 '17 at 14:43
  • No, it is not a category. It is just a type of product. There are multiple categories being used, so just assigning a class is not possible in the case. – Domas Dec 12 '17 at 16:58
0

Copy: /app/code/core/mage/catalog/model/url.php

To: /app/code/local/mage/catalog/model/url.php

replace public function getProductRequestPath($product, $category) with following

public function getProductRequestPath($product, $category)
{
    if ($product->getUrlKey() == '') {
        $urlKey = $this->getProductModel()->formatUrlKey($product->getName());
    } else {
        $urlKey = $this->getProductModel()->formatUrlKey($product->getUrlKey());
    }
    $storeId = $category->getStoreId();
    $suffix  = $this->getProductUrlSuffix($storeId);
    $idPath  = $this->generatePath('id', $product, $category);
    /**
     * Prepare product base request path
     */
    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;
    }

    if (strlen($requestPath) > self::MAX_REQUEST_PATH_LENGTH + self::ALLOWED_REQUEST_PATH_OVERFLOW) {
        $requestPath = substr($requestPath, 0, self::MAX_REQUEST_PATH_LENGTH);
    }

    $this->_rewrite = null;
    /**
     * Check $requestPath should be unique
     */
    if (isset($this->_rewrites[$idPath])) {
        $this->_rewrite = $this->_rewrites[$idPath];
        $existingRequestPath = $this->_rewrites[$idPath]->getRequestPath();

        if ($existingRequestPath == $requestPath . $suffix) {
            return $existingRequestPath;
        }

        $existingRequestPath = preg_replace('/' . preg_quote($suffix, '/') . '$/', '', $existingRequestPath);
        /**
         * Check if existing request past can be used
         */
        if ($product->getUrlKey() == '' && !empty($requestPath)
            && strpos($existingRequestPath, $requestPath) === 0
        ) {
            $existingRequestPath = preg_replace(
                '/^' . preg_quote($requestPath, '/') . '/', '', $existingRequestPath
            );
            if (preg_match('#^-([0-9]+)$#i', $existingRequestPath)) {
                return $this->_rewrites[$idPath]->getRequestPath();
            }
        }

        $fullPath = $requestPath.$suffix;
        if ($this->_deleteOldTargetPath($fullPath, $idPath, $storeId)) {
            return $fullPath;
        }
    }
    /**
     * Check 2 variants: $requestPath and $requestPath . '-' . $productId
     */
    $validatedPath = $this->getResource()->checkRequestPaths(
        array($requestPath.$suffix, $requestPath.'-'.$product->getId().$suffix),
        $storeId
    );

    if ($validatedPath) {
        return $validatedPath;
    }
    /**
     * Use unique path generator
     */
    return $this->getUnusedPath($storeId, $requestPath.$suffix, $idPath);
}

find product-prefix and replace it with your prefix.

flush cache and reindex.

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.