我的旧网站上有成千上万的无valid产品,带有诸如此类的URL http://example.com?product_id=123456.

它们都已被搜索引擎索引,现在在 Magento 中解析为 404。我不可能为每一个都创建一个重定向。

如何让他们解析到我的主页?

有帮助吗?

解决方案

在这种情况下,您可以使用 事件/观察者 并在活动中 controller_front_init_before 解雇一名观察员。

首先我猜想 您的旧产品 ID 和新产品 ID 相同.

在那 observer check query string($_SERVER['QUERY_STRING']) content

?product_id=产品ID 图案 in url 然后基本这个 redirect to current system product url.

例子:

认为,

旧系统产品网址: http://example.com?product_id=123456

新系统产品网址: http://example.com/abx.html

我的代码将被重定向 http://example.com?product_id=123456http://example.com/abx.html 使用 301 重定向。

301重定向,你将解决SEO问题。

这里是config.xml代码:

 <global>
    <models>
      <magento67130>
        <class>StackExchange_Magento67130_Model</class>
        <resourceModel>magento67130_mysql4</resourceModel>
      </magento67130>
    </models>
    <events>
      <controller_front_init_routers> <!-- identifier of the event we want to catch -->
        <observers>
          <controller_front_init_before_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento67130/observer</class> <!-- observers class alias -->
            <method>redirectionProduct</method>  <!-- observer's method to be called -->
          </controller_front_init_before_handler>
        </observers>
      </controller_front_init_routers>
    </events>
  </global>

Observer.php代码如下:

<?php
class StackExchange_Magento67130_Model_Observer
{

    public function redirectionProduct(Varien_Event_Observer $observer)
    {
        return;
        if($this->_getQueryString()){
            print_r($this->_getQueryString());

                // Check query string exits in product_id
            if(strpos($this->_getQueryString(),'product_id=')!== false){
                /* get Product ifd from query string */
                 $productid=str_replace('product_id=','',$this->_getQueryString());
                if(!empty($productid)):
                $product= Mage::getModel('catalog/product')->load($productid);
                 if ($product->getId()) {
                    echo $product->getProductUrl();
                     Mage::app()->getResponse()
                        ->setRedirect($product->getProductUrl(), 301)
                             ->sendResponse();

                             return;
                 }
                endif;
            }

        }
        //die();

    }
    /* Check here Request query string */
    protected function _getQueryString()
        {
            if (!empty($_SERVER['QUERY_STRING'])) {
                $queryParams = array();
                parse_str($_SERVER['QUERY_STRING'], $queryParams);
                $hasChanges = false;
                foreach ($queryParams as $key => $value) {
                    if (substr($key, 0, 3) === '___') {
                        unset($queryParams[$key]);
                        $hasChanges = true;
                    }
                }
                if ($hasChanges) {
                    return http_build_query($queryParams);
                } else {
                    return $_SERVER['QUERY_STRING'];
                }
            }
            return false;
        }
}

其他提示

如果在数据库中找不到产品,您必须创建一个小的自定义扩展来重定向

在这里我给出了工作解决方案,它完美地为我工作

如何将所有 404 Magento 页面转发到首页?

希望这也对你有用。

许可以下: CC-BY-SA归因
scroll top