1

I want to redirect all product URLs without a category path to their category path URLs for SEO purposes.

Example - Redirect mywebsite.com/product-name.html to mywebsite.com/category1/sub-category/product-name.html.

I am thinking to override the observer on the event when a url is hit in magento. How to find that event?

Please help.

0

Please change your catalog configuration.

Admin -> Stores -> Configuration -> Catalog -> Catalog -> Search Engine Optimization -> Use Categories Path for Product URLs to NO

php bin/magento indexer:reindex

php bin/magento cache:clean

0

Override product controller in Vendor/Module/Product/View.php with below code -

namespace [Vendor_Name][Module_Name]\Controller\Product; use Magento\Framework\Exception\NoSuchEntityException; class View extends \Magento\Catalog\Controller\Product\View {

public function execute($coreRoute = null) {

$productid = (int)$this->getRequest()->getParam('id', false);
$urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
$initialUrl =$urlInterface->getCurrentUrl();

  if($productid){
    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
    $categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
    $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
    $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');


    $product = $productRepository->getById($productid);
    $productUrlKey = $product->getUrlKey();

    $baseurl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB,true); 

    $categoryIds = $product->getCategoryIds();
    $categories = $categoryCollection->create()
                     ->addAttributeToSelect('*')
                     ->addAttributeToFilter('entity_id', $categoryIds);

       foreach ($categories as $category){
            $categoryNames[] = $category->getUrlKey();
         }

      if(strpos($initialUrl,$categoryNames[0])===false){       
        if (array_key_exists(1,$categoryNames)) {
            $productNewPath = $baseurl.$categoryNames[1].'/'.$categoryNames[0].'/'.$productUrlKey.'.'.'html';
          }        
        else{
           $productNewPath = $baseurl.$categoryNames[0].'/'.$productUrlKey.'.'.'html';
         }

            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setUrl($productNewPath);
              return $resultRedirect;

        }

$params = new \Magento\Framework\DataObject();
$params->setCategoryId($categoryIds);
$page = $this->resultPageFactory->create();
        $this->viewHelper->prepareAndRender($page, $productid, $this, $params);
return $page;


 }

} }

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.