3

I have created a controller for my custom module. My controller path is like http://www.example.com/test/index/index which is displaying page correctly. I want to perform URL-rewrite for converting /test/index/index to /test.html

Based on the first answer, I have modified my code as below:

I tried to do something like following, in my config.xml

<frontend>
    <routers>
        <test>
            <use>standard</use>
            <args>
                <modules>
                    <module>Pfay_Test</module>
                    <frontName>test</frontName>
                </modules>
            </args>
        </test>
    </routers>
</frontend>

<global>
    <controller_front_init_routers>
        <observers>
            <test>  <!-- observer identifier -->
                <class>Pfay_Test_Controller_Router</class>
                <method>initControllerRouters</method>
            </test>
        </observers>
    </controller_front_init_routers>

</global>

In /app/code/local/Pfay/Test/Controller/Router.php

<?php
     class Pfay_Test_Controller_Router extends  Mage_Core_Controller_Varien_Router_Abstract{
/**
 * Initialize Controller Router
 *
 * @param Varien_Event_Observer $observer
 */

public function initControllerRouters($observer){
    $front=$observer->getEvent()->getFront();
    $front->addRouter('test',$this);

}


/* validate and modify the  request
 *  Params Zend_Controller_Request_Htt
 */
public function match(Zend_Controller_Request_Http $request){

    /* If Magento Magento is not install then 
     * redirect to installer url
     */
    If(!Mage::isInstalled()):
        Mage::app()->getFrontController()->getResponse()
            ->setRedirect(Mage::getUrl('install'))
            ->sendResponse();
        exit;

    endif;
    $requestPathInfo=trim($request->getPathInfo(),'/');
    Mage::log('aaa'.$requestPathInfo.'StoreId'.Mage::app()->getStore()->getId(), null, 'logfile.log');
    /* check -review-form not exit
     * then immediate return false
     */
    if($requestPathInfo!='test.html'):
        return  false;
    endif;


    $request->setModuleName('test')
        ->setControllerName('index')
        ->setActionName('index');
    $request->setAlias(
        Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
        'test.html'
    );


    return  true;
  }
 }

In /app/code/local/Pfay/Test/Model/Observer.php

<?php
class Pfay_Test_Model_Observer{

public function add($observer){

    $identifier=$observer->getEvent()->getCondition();
    Mage::log('aaa', null, 'logfile.log');

     }

   }

But when I type http://www.example.com/test.html , it is still showing 404 error.Can anybody hint out where it is going wrong?

I am using Magento 1.9, Apache 2.4 and mod_rewrite is enabled.

2

Easiest,solution, Go to Catalog-> URL Rewrite Management.

Click ‘Add URL Rewrite’ Select “Create URL Rewrite:” drop-down to “Custom” Then enter “ID Path”: category/55 or anything without space (this may be not available in Magento EE), Enter “Request Path as "test.html* target test/index/index

In this case,it will better idea to create custom route like Mage_Cms module.

Where depends on request path using Custom route match internally set the request path

  • modules ->CustommoduleNameSpace_ModuleName

  • controller ->indexController.php

  • Action ->indexAction.

Customer will see that like

http://www.domain.com/test.html

but internally it hit to

http://www.domain.com/test/index/index/

Follow the link to implement this functonality

Controller code should be :

<?php
class CustommoduleNameSpace_ModuleName_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract{
    /**
     * Initialize Controller Router
     *
     * @param Varien_Event_Observer $observer
     */

    public function initControllerRouters($observer){
        $front=$observer->getEvent()->getFront();
        $front->addRouter('test',$this);

    }


    /* validate and modify the  request
     *  Params Zend_Controller_Request_Htt
     */
    public function match(Zend_Controller_Request_Http $request){

        /* If Magento Magento is not install then 
         * redirect to installer url
         */
        If(!Mage::isInstalled()):
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect(Mage::getUrl('install'))
                ->sendResponse();
                 exit;

        endif;
        $requestPathInfo=trim($request->getPathInfo(),'/');
        Mage::log('aaa'.$requestPathInfo.'StoreId'.Mage::app()->getStore()->getId(), null, 'logfile.log');
        /* check -review-form not exit
         * then immediate return false
         */ 
        if($requestPathInfo!='test.html'):
            return  false;
        endif;


        $request->setModuleName('test') 
                    ->setControllerName('index')
                 ->setActionName('index');
        $request->setAlias(
                Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                'customindentifier'
        );


        return  true;

    }


}
  • In the code Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS, 'customindentifier' what 'customindentifier' means?I replaced it with 'test.html',btw it's working now. – Hiranya Sarma Jan 12 '16 at 7:28
  • 1
    little busy .Wil be reply after 5PM – Amit Bera Jan 12 '16 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.