1

As a part of the SEO optimizations, when a customer go to a category page and if there is no products in the category, can we redirect it to a 301?

Eg: As a part of -ve testing, if we load the category page with a non valid page number, then magento will show the message as "We can't find products matching the selection.". So instead of this i want to either redirect to 301 OR redirect 404 is also fine in this case. Do we have any solution for this?

1 Answer 1

2

I think you should implement this functionality with 302 instead of 301 because it's a permanent redirect.

Here is the implementation with 302 redirects:

  1. Create a module Vendor_Module

  2. Create registration.php:

    <?php
    
    use Magento\Framework\Component\ComponentRegistrar;
    
    ComponentRegistrar::register(
        ComponentRegistrar::MODULE,
        'Vendor_Module',
        __DIR__
    );
    
  3. Create etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Vendor_Module"/>
    </config>
    
  4. Create etc/frontend/di.xml

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\Catalog\Block\Product\ListProduct">
            <plugin name="redirect_empty_category" type="Vendor\Module\Plugin\ListProduct"/>
        </type>
    </config>
    
  5. Create Plugin/ListProduct.php

    <?php
    
    declare(strict_types=1);
    
    namespace Vendor\Module\Plugin;
    
    use Laminas\Http\Response;
    use Magento\Eav\Model\Entity\Collection\AbstractCollection;
    use Magento\Framework\App\ResponseInterface;
    use Magento\Framework\UrlInterface;
    
    class ListProduct
    {
    
        /**
         * @param ResponseInterface $response
         * @param UrlInterface $url
         */
        public function __construct(
            private readonly ResponseInterface $response,
            private readonly UrlInterface $url
        ) {
        }
    
        /**
         * @param \Magento\Catalog\Block\Product\ListProduct $subject
         * @param AbstractCollection $result
         *
         * @return AbstractCollection
         */
        public function afterGetLoadedProductCollection(
            \Magento\Catalog\Block\Product\ListProduct $subject,
            AbstractCollection $result
        ): AbstractCollection {
            if ($result->getSize() === 0) {
                $this->response->setRedirect(
                    $this->url->getUrl('your-redirect-url'), // change this to the URL you want to redirect to.
                    Response::STATUS_CODE_302 // even though this is set to 302, if you decide you can go with 301 as well.
                );
            }
    
            return $result;
        }
    }
    
1
  • 1
    It worked perfectly. Thank you so much @Ahmad for your support and especially for the detailed explanation.
    – amesh
    Jul 20 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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