Magento custom search engine friendly (SEF) URL.

on

Magento providing us to create search engine friendly (SEF) URL’s from admin->catalog->URl Rewrite Manager. This awesome feature in Magento admin to create SEO friendly URL’s. We create SEF URL manually, Manually sounds stupid for 1000 URL’s. So while creating front-end modules we need to take a notice at SEF URL using programming coding. So Let’s see, how to create the custom search engine URL programmatical.

We create a method called custom_sef_url in your custom module helper class. Basically helper classes will contain utility methods for general tasks to do.

Custom SEF helper method

class <Namespace>_<Module>_Helper_Data extends Mage_Core_Helper_Abstract
{

function custom_sef_url($id_path, $request_path, $target_path, $store_id ="0", $is_system=false){

$isrewrite = Mage::getModel('core/url_rewrite')->load($id_path,id_path);

    if($isrewrite->url_rewrite_id !=""){
      throw new Exception("Your Id path must be unique, given id path already in use !");
    }

      $rewrite = Mage::getModel('core/url_rewrite');
      $rewrite->setStoreId($store_id)
		->setIdPath($id_path)
		->setRequestPath($request_path)
		->setTargetPath($target_path)
		->setIsSystem($is_system)
		->save();     	
return;
}

}

Where should I call Custom SEF helper method?

Normally we need to create the custom URL once the new item is created, so we need to call this method in your “saveAction()” function after saving process. Let’s take FAQ items as example.

public function saveAction()
{

// .... faq saving process

try{    
  $id_path = "faq/".$id;  // Id path must be unique
  $request_path = "fag/".$fag_title;
  $target_path  = "fag/index/index/id/".$id;

   Mage::helper('<Module>')->custom_sef_url($id_path, $request_path, $target_path);    
}
catch (Exception $e) {

 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());

}

}

I used this code year before in my magento application not tested now, so be ready to debug if any issue happen. All your comments are welcome.

Posted in Magento and tagged , , by .

About Gowri

I am professional web developer with 8+ years experience. PHP, jQuery, WordPress, Angular and Ionic are my key skills in web development. I am working with strong enthusiastic team with spirit. We provide all web related solution like HTML/CSS development, Web graphic design and Logo.