Magento redirect route in Custom Module and Observer

I was asked to redirect all routes(except admin) in Magento 1.9.2 to a custom module‘s frontend.  With Observer pattern, it is very easy to achieve it.

In Magento, when the front page layout beginning to display, an event “controller_front_send_response_before” will occur, and we will subscribe to this event to trigger our function for redirection.

To subscribe to this event, add the following xml tags in etc/config.xml in our custom module directory in the global tag.

This is what I guess the meaning of this xml tag, but I may be wrong: Magento will use singleton method, get model observer, and call customRedirect() function.  (e.g. Mage::getSingleton(‘custom_module/observer’)->customRedirect() )

Since we will need a new model “Observer”, in our custom module directory, under Model folder, create a file call “Observer.php”.

In customeRedirect() function, my code will check if this is admin path, or custommodule path.  If pattern does not match, it will redirect to custommodule route.

if you want to redirect to HTTPS route, you will need to provide the second parameter in setRedirect function like this:

setRedirect(Mage::getUrl(“customemodule/index/index”, array(‘_forced_secure’ => true);

 

 

Read More