Using Magento Xml To Rewrite Url
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:
www.your-website.com/manufacturer/index/index/brand/sony
This default URLs in Magento are like this where,
manufacturer = module frontend URL
index = IndexController
index = index Action function
brand/sony = request parameters
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.
public function rewrite()
{
$request = $this->getRequest();
$config = Mage::getConfig()->getNode('global/rewrite');
if (!$config) {
return;
}
foreach ($config->children() as $rewrite) {
$from = (string)$rewrite->from;
$to = (string)$rewrite->to;
if (empty($from) || empty($to)) {
continue;
}
$from = $this->_processRewriteUrl($from);
$to = $this->_processRewriteUrl($to);
$pathInfo = preg_replace($from, $to, $request->getPathInfo());
if (isset($rewrite->complete)) {
$request->setPathInfo($pathInfo);
} else {
$request->rewritePathInfo($pathInfo);
}
}
}
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.
protected function _processRewriteUrl($url)
{
$startPos = strpos($url, '{');
if ($startPos!==false) {
$endPos = strpos($url, '}');
$routeName = substr($url, $startPos+1, $endPos-$startPos-1);
$router = $this->getRouterByRoute($routeName);
if ($router) {
$fronName = $router->getFrontNameByRoute($routeName);
$url = str_replace('{'.$routeName.'}', $fronName, $url);
}
}
return $url;
}
Conclusion
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.
by: Pamela adreson
Ocatalogs Opencartapp A Complete Mobile Commerce Solution! Choosing Where To Buy Luggage For Your Motorbike Ghost Hunting Event At Alton Towers With Simply Ghost Nights, 9th June 2012 Irans Retail Market Surging At 14% Growth A Glance At Honda Bikes Price Contemporary Chandeliers Trends What Points Need To Be Considered Before Renting An Office Space In Brisbane How To Choose Bandeau Bathing Suit What Have Been The Reasons For A Rise In The Flights To Doha Stop Smoking Forever With These Super Tips Leaves Of Some Palms Cash Help To Ease All Your Worries Professional Laser Training Courses