subject: Using Magento Xml To Rewrite Url [print this page] Magento is a well acclaimed E-commerce solution, with features that provide flexibility and enhanced functionality to the website. Magento developers have build competency through the years to customize Magento.
This document describes how to rewrite simple URL in customize Magento using config.xml. Such a method provides easy creation of modules and gives fancy URL to them.
Example for Creation of Modules and Fancy URL
This can be explained with the help of an example. Lets assume that a module is being developed displaying a list of products of a particular manufacturer. Here manufacturer is a product drop down product attribute. On presuming that the frontend URL of our module is manufacturer, the URL of a particular manufacturer page would look like this:
But a URL like www.your-website.com/manufacturer/sony is better that work similarly to the URL mentioned above. Traditionally this is done by .htaccess URL rewrites. But this article reveals the method of rewriting using config.xml controller.
Rewriting Using config.xml Controller
Code added to the tag of config.xml file
This can be done by adding following code inside the tag of config.xml file of the module.
Here Magento developers need to rewrite /manufacturer/(.*)/ to manufacturer/index/index/manufacturer/$1/.
The tag, which is the source URL, takes in any regular expression and in the tag the destination URL is introduced. $1, $2 etc is used for variable substitution and works same as the php preg_match function.
Importance of the tag 1
The tag 1 is important; as it makes customize Magento use the correct layout handle used in layout xml files. This means that if the tag 1is not included, the controller will not read the xml tag related to . It will read something else depending on the URL.
$this->loadLayout();
$this->renderLayout();
These rewriting process takes place in the code located in class Mage_Core_Controller_Varien_Front, rewrite() function.
As Magento itself uses the preg_replace() function, so all rules applicable to preg_replace() can be used for the rewrites also.
Exclusion of Curly Braces
The only exception is that, regular expressions cannot have { and } in them, the curly bracket regex wont work. Its because Magento developers need to use curly brackets for internal processing. The code of the _processRewriteUrl() function replaces the content inside curly braces with route name.
Magentos URL rewrite functionality enables to create search engine friendly URL for the custom module. Hopefully you where able to understand the rewrite trick which can be used very effectively to have nice fancy URLs in Magento modules.