How to rewrite URL in Magento using config.xml


By default, Magento product URL looks very complicated and not SEO friendly, in this article, we’re going to learn how to do simple URL rewrite in Magento using config.xml. By using config.xml method,  you can create friendly URL without using .httaccess rewrites. This is very helpful when building a Magento module

Let’s say that you’re a Magento developer and you’re developing a Magento module which will show a list of products of a specific brand. Here brand is a product attribute, assuming that our module is named “brand”. By default, the url of brand page will be like this

This is how default URL’s are in magento where,

  • brand = module frontend url
  • index = IndexController
  • index = index Action function
  • apple = request parameters

How can we rewrite URL so it will looks like this

This is SEO friendly version that we all want to create. In general, we use .htaccess URL rewrites to make this, however, in this article, I will explain how to rewrite using config.xml controll.

How to do it

Simply, open config.xml file of your Magento module and put this code inside <global> tag.


With the code above, we tell Magento to rewrite /brand\/(.*)/  to  brand/index/index/brand/$1/

The <from>  tag is the source url and in the <to> tag we set the new url. $1,$2 etc is used for variable substitution, they work just like   php preg_match  function.
The line <complete>1</complete>  is important, it requests magento to use the correct layout handle used in layout xml files. If you don’t put <complete>1</complete> , then in your controller when you do

Magento will not read xml tag related to <brand_index_index>  but something else depending on your URL.

How this config.xml works? Let’s take a deeper look inside Magento class  Mage_Core_Controller_Varien_Front , rewrite() function, there you will find the code

Magento use  preg_replace()  function itself, so all rules applicable to preg_replace() can be used for our rewerites.

After walking through the explanation, I hope you understand the rewrite trick in config.xml file, which can be applied in creating friend URL version in Magento extension

Rate this post


About Author

Brian is a Magento developer with 5 years of experience with customizing Magento extensions, Magento templates. He's main author at Magento explorer

Leave A Reply