【问题标题】:How to change magento category url to short url(friendly for google)?如何将magento类别网址更改为短网址(对谷歌友好)?
【发布时间】:2011-03-11 02:40:33
【问题描述】:

我使用magento 1.4.1.1,在后端配置中,有一个选项:删除产品的类别url,好吧,产品url是http://www.yourdomain.com/products.html,这是完美的功能。但是类别 url 仍然有层,如何删除类别 url 的父 url?因此,当我尝试更改类别层时,就可以了。

提前感谢,对不起我的英语不好。

【问题讨论】:

  • 请确保重建目录缓存。

标签: url magento categories


【解决方案1】:

编辑文件 /app/code/core/Mage/Catalog/Model/Url.php 以注释这些行(见下文)如果不搜索文件中的代码,则应为 1.4 的第 673 到 679 行。 然后在 admin -> system -> Index management 中刷新 url rewrites

//if (null === $parentPath) {
                   //$parentPath = $this->getResource()->getCategoryParentPath($category);
               //}
               //elseif ($parentPath == '/') {
                   $parentPath = '';
               //}

【讨论】:

  • 太棒了!只是想评论并说此修复程序也适用于 Magento 1.7 :)
  • 不要更改核心代码。重写模型并在本地代码池中进行更改。
  • @Nick 说得再多也不过分。
【解决方案2】:

我搜索了一下,但没有找到任何有用的东西,所以我最终得到了这个解决方案

第一个我创建了一个观察者,在保存类别后运行, app/code/local/namespace/module/etc/config.xml

<events>
    <catalog_category_save_commit_after>

      <observers>

        <namespace_module_Model_observer>

            <type>singleton</type>
            <class>namespace_module/observer</class>
            <method>setUrlRedirect</method>

        </namespace_module_Model_observer>

      </observers>

    </catalog_category_save_commit_after>
</events>

然后在我的观察者中,我添加了一个自定义 url 重写,使我们的类别 url 可以直接访问(example.com/deeply-layered-catogry.html)

public function setUrlRedirect($observer) {

    $e = $observer->getEvent();
    $c = $e->getCategory();
    // getting updated data, 
    $data = $observer->getDataObject()->getData();

    $c = Mage::getModel("catalog/category")->load($c->getId());
    $url = $c->getUrl();

    $handle = $data['url_key'];
    $p = 'catalog/category/view/id/' . $c->getId(); /*$handle . ".html";*/
    $id = 'seo-frindly/cat-' . $c->getId() .'.html';
    $urlMdoule =  Mage::getModel('core/url_rewrite');
    $storeId =  Mage::app()->getStore()->getStoreId();


    if (  $urlMdoule->loadByIdPath($id)->getId() ) {
        // update
        $o = $urlMdoule->loadByIdPath($id);

        $o->setIsSystem(0)
        ->setStoreId($storeId)   
        ->setOptions('no')
        ->setTargetPath( $p )// Put the actual path
        ->setRequestPath( $handle .'.html')
        ->setRedirect(false)
        ->save();

    } else {

       // new
       $urlMdoule->setIsSystem(0)
        ->setStoreId($storeId)   
        ->setOptions('no')  
        ->setIdPath($id)
        ->setTargetPath( $p )// Put the actual path
        ->setRequestPath( $handle .'.html')
        ->setRedirect(false)
        ->save();
    }


    return;

}

现在,当您保存类别时,可以从较短的 url 访问它,

只需在 head 中添加 rel="canonical" 属性,这样 google 就会索引较短的 seo 友好链接

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-12
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多