
MAGENTO 1.X – REDIRECT TO URL IN FRONTEND
Sometimes you will need to create a validation once you are entering through an url, and then redirect the user to another site/section.
In this case, we want to get those uppercase non existing urls, and redirect the user to the corresponding existing lowercase url (Case insensitive).
To do so, i created the following local extension, catching the controller_action_predispatch event.
- app/etc/modules/Offset101_UrlCustomization.xml
123456789<?xml version="1.0"?><config><modules><Offset101_UrlCustomization><active>true</active><codePool>local</codePool></Offset101_UrlCustomization></modules></config> - app/code/local/Offset101/UrlCustomization/etc/config.xml
12345678910111213141516171819202122232425262728<?xml version="1.0"?><config><modules><Offset101_UrlCustomization><version>0.0.1</version></Offset101_UrlCustomization></modules><global><models><offset101urlcustomization><class>Offset101_UrlCustomization_Model</class></offset101urlcustomization></models></global><frontend> <!-- You can change it to adminhtml in case you want those backend urls --><events><!-- observe the event --><controller_action_predispatch><observers><offset101urlcustomization><class>offset101urlcustomization/observer</class><method>changeToLowercaseURL</method></offset101urlcustomization></observers></controller_action_predispatch></events></frontend></config> - app/code/local/Offset101/UrlCustomization/Model/Observer.php
12345678910111213141516171819202122232425<?phpclass Offset101_UrlCustomization_Model_Observer {public function changeToLowercaseURL($observer) {// If the url do not existsif ($observer->getEvent()->getControllerAction()->getFullActionName() == 'cms_index_noRoute') {// Validate if i make a redirection before, to avoid infinit loops in non existing lower case urlsif($_SESSION["countredirects"]){session_unset($_SESSION["countredirects"]);return;}// Create a variable to validate further infinit loops$_SESSION["countredirects"] = true;$requestedString = $observer->getEvent()->getControllerAction()->getRequest()->getRequestString();$newURL = rtrim(Mage::getBaseUrl(), '/') . strtolower($requestedString);Mage::app()->getFrontController()->getResponse()->setRedirect($newURL);Mage::app()->getResponse()->sendResponse();exit;}}}?>