Magento 防止在产品URL中包含类别URL键的自动URL重写

Magento 防止在产品URL中包含类别URL键的自动URL重写,magento,url-rewriting,Magento,Url Rewriting,每当我创建新产品时,Magento都会自动创建不必要的URL重写,其中包括每个类别和子类别的组合,这些组合使用产品路径中类别的URL键。例如,对于具有以下类别的产品产品名称: 类别 类别>子类别 类别>子类别>第三类 …Magento将使用以下请求路径自动创建URL重写: /类别/产品名称 /类别/子类别/产品名称 /类别/子类别/第三方/产品名称 …以及使用请求路径创建正在使用的URL重写: /product name 我的问题是,即使我在: 系统>配置>目录>搜索引擎优化 …如何阻止自动创建

每当我创建新产品时,Magento都会自动创建不必要的URL重写,其中包括每个类别和子类别的组合,这些组合使用产品路径中类别的URL键。例如,对于具有以下类别的产品
产品名称

类别
类别>子类别
类别>子类别>第三类

…Magento将使用以下请求路径自动创建URL重写:

/类别/产品名称
/类别/子类别/产品名称
/类别/子类别/第三方/产品名称

…以及使用请求路径创建正在使用的URL重写:

/product name

我的问题是,即使我在:

系统>配置>目录>搜索引擎优化

…如何阻止自动创建这些额外的URL重写?

现在,我再次意识到,该网站没有链接到网站上任何地方的这些附加路径,但如果出于某种原因,搜索引擎发现:

http://example.com/category/subcategory/third/product-name

…这会加载!我担心这会导致重复的内容被搜索引擎索引。由于产品URL的
使用类别路径
设置设置为
,并且网站上指向产品的所有链接都指向:

http://example.com/product-name

…我想阻止Magento自动创建这些不必要的URL重写

作为参考,我尝试将
core\u url\u rewrite
表截断为零(基本上清空它),并在
System>索引管理中重新索引目录url rewrite。这仍然会导致Magento自动创建这些不必要的URL重写

另外,作为参考,我使用的是Magento社区1.9.1


请告知!非常感谢您的帮助。

我建议您不要尝试禁用此内置功能,而是。如果您的旧版本Magento没有此选项

但是,如果仍然想删除它,他们可能会创建一个扩展来扩展
Mage\u Catalog\u Model\u Url
,这样做:

class My_Catalog_Model_Url extends Mage_Catalog_Model_Url
{
     public function refreshProductRewrite($productId, $storeId = null)
     {
         if (is_null($storeId)) {
             foreach ($this->getStores() as $store) {
                 $this->refreshProductRewrite($productId, $store->getId());
             }
             return $this;
         }

         $product = $this->getResource()->getProduct($productId, $storeId);
         if ($product) {
             $store = $this->getStores($storeId);
             $storeRootCategoryId = $store->getRootCategoryId();

             // List of categories the product is assigned to, filtered by being within the store's categories root
             // CUSTOMIZATION: Ignore product categories if the 'catalog/seo/product_use_categories' config setting is false.
             if (Mage::getStoreConfigFlag('catalog/seo/product_use_categories', $storeId)) {
                 $categories = $this->getResource()->getCategories($product->getCategoryIds(), $storeId);
             } else {
                 $categories = array();
             }
             $this->_rewrites = $this->getResource()->prepareRewrites($storeId, '', $productId);

             // Add rewrites for all needed categories
             // If product is assigned to any of store's categories -
             // we also should use store root category to create root product url rewrite
             if (!isset($categories[$storeRootCategoryId])) {
                 $categories[$storeRootCategoryId] = $this->getResource()->getCategory($storeRootCategoryId, $storeId);
             }

             // Create product url rewrites
             foreach ($categories as $category) {
                 $this->_refreshProductRewrite($product, $category);
             }

             // Remove all other product rewrites created earlier for this store - they're invalid now
             $excludeCategoryIds = array_keys($categories);
             $this->getResource()->clearProductRewrites($productId, $storeId, $excludeCategoryIds);

             unset($categories);
             unset($product);
         } else {
             // Product doesn't belong to this store - clear all its url rewrites including root one
             $this->getResource()->clearProductRewrites($productId, $storeId, array());
         }

         return $this;
     }
}

这不仅仅是关于规范链接,问题主要是另一个:爬行预算。你不想浪费你的爬行预算,所以不必要的网址需要去

您应该修改core_url_rewrite by shell脚本中的每个条目,该脚本:

  • 系统=1吗
  • 产品标识不为空
  • 类别id不为空
这就是你设定的:

  • 目标路径=直接产品url
  • 选项=卢比
现在您创建了301重定向到真实页面,只剩下一个问题:

如果产品没有类别产品URL,那么如果通过后端配置设置关闭该功能,则不会创建其他URL,这就是我们想要的。 但是,如果一个产品还具有类别产品url,并且您将该产品添加到一个类别中,那么仍然会创建一个新的类别产品url。因此,您需要通过重写/扩展Mage_Catalog_Model_Url来更改一种方法:

 /**
     * Refresh product rewrite
     *
     * @param Varien_Object $product
     * @param Varien_Object $category
     * @return Mage_Catalog_Model_Url
     */
    protected function _refreshProductRewrite(Varien_Object $product, Varien_Object $category)
    {
        //FIX: DONT ADD CATEGORY-PRODUCT-URL - MIGHT HAPPEN IF CATEGORY-PRODUCT-URL EXIST YET FOR THIS PRODUCT
        if (Mage::getStoreConfigFlag('catalog/seo/product_use_categories')) {
            if ($category->getId() && $product->getId()) {
                return $this;
            }
        }

        parent::_refreshProductRewrite($product, $category);
    }

当管理员中有一个简单的选择要更改以激活规范url时,为什么要对模板进行黑客攻击?系统>>配置>>目录>>搜索引擎OPTIMIZATION@b.enoit.be-接得好。该博客的解决方案仅适用于没有该选项的旧版本。我已经更新了我的答案,并链接到另一个博客,展示了如何启用内置选项(如果可用)。非常有用!非常感谢。我想知道规范金属链接的设置是为了什么。然而,对于我最初的问题,是否有一种方法可以完全按照原则来完成我的要求?核心Magento代码导致创建这些链接……当然这是可以更改的。@ZacharyBeschler-一切都可以更改。我用另一种解决方案更新了我的答案,这种解决方案可能会奏效。