简体   繁体   中英

Magento seo friendly url in custom module

I would like to create porfolio module, but I need it to be seo friendly, so url should look like:

example.com/portfolio/projectid

where portfolio - module, so it will be index action projectid - based on field alias

its easy to create example.com/portfolio but how to create controller, or detect what is after? The same URL hierarchy is in products, so it's category/product

Anybody have idea how to do it?

Below code works for item/id. you can change with your requirement.

                    $model = Mage::getModel('items/items');

        /*Rewrite */
        $isSystem = 0; // set 0 for custom url as we have created custom for profile extension

        $_itemId = $model->getModuleItemId();//module_item_id field
        $_itemName = $model->getTitle();//title field
        $_itemName = strtolower(str_replace(" ", "", $_itemName));



        // save profile view url rewrite
        $viewIdPath = 'item/id'.'/'.$_itemId;

        $viewRequestPath = 'items/'.$_itemName;
        $viewTargetPath = 'items/index/item/id/'.$_itemId;//controller is itemsController.php, Action is itemAction() 

        $_coreUrlRewrite = Mage::getModel('core/url_rewrite');
        $_coreUrlRewrite->load($viewIdPath, 'id_path'); // check if item path already saved? If yes, $_coreUrlRewrite will contain existing data.

        $_coreUrlRewrite->setStoreId($_storeId)
        ->setIdPath($viewIdPath)
        ->setRequestPath($viewRequestPath)
        ->setTargetPath($viewTargetPath)
        ->setIsSystem($isSystem)
        ->save();
        if(isset($viewRequestPath)){
            $model->setUrl($viewRequestPath);
            $model->save();
        }

        /*Rewrite End*/

You can find more info here & here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM