How to change magento category url to short url(friendly for google)?

I use magento 1.4.1.1, at the backend configuration, there’s a option: remove category url for products, well, the product url was http://www.yourdomain.com/products.html, this was perfect feature. but the category url was still has layer, how can I remove the parent url for category url? so when I try to change the categories layer, then it’s okay.

Thanks advance, sorry for my poor english.

2 thoughts on “How to change magento category url to short url(friendly for google)?”

  1. Edit the file /app/code/core/Mage/Catalog/Model/Url.php as to comment these lines (see below) Should be lines 673 to 679 for 1.4, if not search for the code in the file.
    Then refresh url rewrites in admin -> system -> Index management

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

  2. i search a bit but didn’t find anything helpful, so i end up with this solution

    1st i create a observer the run after the category is saved,
    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>
    

    then in my observer i add a custom url rewrite that make our category url directly accessible (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;
    
    }
    

    now when ever you save the category, it will be accessible from the shorter url,

    just add the rel=”canonical” attribute in head so google will index the shorter seo firendly links

Leave a Reply

Your email address will not be published. Required fields are marked *