Magento无商品分类页及无效页码页面的301/404跳转需求
Magento 空分类/无效页码页面替换为301/404重定向方案
一、处理分类本身无商品的情况
通过创建插件拦截分类页面的加载逻辑,判断商品数量后执行重定向或返回404:
- 创建插件文件
app/code/YourVendor/YourModule/Plugin/Catalog/Controller/Category/View.php:
<?php namespace YourVendor\YourModule\Plugin\Catalog\Controller\Category; use Magento\Catalog\Controller\Category\View as CategoryView; use Magento\Framework\Controller\Result\RedirectFactory; use Magento\Framework\Controller\Result\NotFoundFactory; class View { protected $redirectFactory; protected $notFoundFactory; public function __construct( RedirectFactory $redirectFactory, NotFoundFactory $notFoundFactory ) { $this->redirectFactory = $redirectFactory; $this->notFoundFactory = $notFoundFactory; } public function aroundExecute(CategoryView $subject, callable $proceed) { $category = $subject->_initCategory(); if (!$category) { return $proceed(); } // 统计分类下商品总数(含子分类商品,可根据需求调整逻辑) $productCount = $category->getProductCollection()->getSize(); if ($productCount === 0) { // 301重定向到首页,可替换为其他目标URL $redirect = $this->redirectFactory->create(); $redirect->setUrl('/'); $redirect->setHttpResponseCode(301); return $redirect; // 若需返回404,替换为以下代码: // return $this->notFoundFactory->create(); } return $proceed(); } }
- 在
app/code/YourVendor/YourModule/etc/frontend/di.xml中注册插件:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Controller\Category\View"> <plugin name="yourmodule_category_empty_redirect" type="YourVendor\YourModule\Plugin\Catalog\Controller\Category\View" sortOrder="10"/> </type> </config>
二、处理分类有效但页码无效的情况
拦截产品列表块的布局准备逻辑,判断页码有效性后执行对应操作:
- 创建插件文件
app/code/YourVendor/YourModule/Plugin/Catalog/Block/Product/ListProduct.php:
<?php namespace YourVendor\YourModule\Plugin\Catalog\Block\Product; use Magento\Catalog\Block\Product\ListProduct; use Magento\Framework\Controller\Result\RedirectFactory; use Magento\Framework\Controller\Result\NotFoundFactory; use Magento\Framework\App\RequestInterface; class ListProduct { protected $redirectFactory; protected $notFoundFactory; protected $request; public function __construct( RedirectFactory $redirectFactory, NotFoundFactory $notFoundFactory, RequestInterface $request ) { $this->redirectFactory = $redirectFactory; $this->notFoundFactory = $notFoundFactory; $this->request = $request; } public function afterPrepareLayout(ListProduct $subject, $result) { $toolbar = $subject->getToolbarBlock(); $collection = $toolbar->getCollection(); $currentPage = (int)$this->request->getParam('p'); $lastPage = $toolbar->getLastPageNum(); // 当页码大于总页数且分类存在商品时 if ($currentPage > $lastPage && $lastPage > 0) { // 301重定向到分类首页 $redirect = $this->redirectFactory->create(); $redirect->setUrl($subject->getCategory()->getUrl()); $redirect->setHttpResponseCode(301); $subject->getResponse()->setRedirect($redirect->getUrl(), 301); // 若需返回404,替换为以下代码: // $subject->getResponse()->setNotFound(true); } return $result; } }
- 在
app/code/YourVendor/YourModule/etc/frontend/di.xml中注册插件:
<type name="Magento\Catalog\Block\Product\ListProduct"> <plugin name="yourmodule_invalid_page_redirect" type="YourVendor\YourModule\Plugin\Catalog\Block\Product\ListProduct" sortOrder="10"/> </type>
三、301 vs 404选择建议
- 若分类后续会添加商品,或无效页码为用户输入失误导致,用301重定向到分类首页/网站首页,保留流量与SEO权重。
- 若分类已废弃且不再使用,直接返回404,明确告知搜索引擎该页面已失效。
内容的提问来源于stack exchange,提问作者Amesh
相关产品推荐
相关产品推荐

