Magento URL Rewrite Process
Magento URL Rewrite Process
We can also add custom rewrites to this table from Admin -> Catalog -> Url Rewrite Management
DB Rewrite Implementation
Let’s see in detail how magento implement these rewrites when we open a URL in browser
1$this->_getRequestRewriteController()->rewrite();
This basically creates an object of class ‘core/url_rewrite_request’ and calls the rewrite function there.
This is what the rewrite() function looks like
1
public function rewrite()
2{
3
4 if (!$this->_request->isStraight()) {
5 $this->_rewriteDb();
6 }
7 $this->_rewriteConfig();
return true;
8}
9
\\\\\\ we can see here _rewriteDb() and _rewriteConfig() functions are called here which manage the db and
config rewrites respectively. Lets look at db rewrite first.
The first thing which happens there is creating of request cases. Request cases is basically an array which has
difference combinations of the request URL priority wise. e.g if the input URL is
“http://yourmagento.com/abc.html?test=1”
The request cases which get generated are
1. abc.html?test=1
2. abc.html/?test=1
3. abc.html
4. abc.html/
Once request cases are generated, magento check’s database for matches using function
1$this->_rewrite->loadByRequestPath($requestCases);
Next we have code which calculates penalty using bitwise operators, but what the code does in short is
return “earlier path in array and concrete store”.
So once we get the final request path, if any magento does the final redirect using
1$this->_processRedirectOptions();
So this is how database redirect options work, let see now how config redirect options work.
Another thing to see is when does magento create these url rewrites
This is done at ‘Mage_Catalog_Model_Indexer_Url’ using the “reindexAll()” function.
1<global>
<rewrite>
2 <designer_url>
3 <from><![CDATA[#^/author/id/#]]></from>
4 <to><![CDATA[/designer/index/index/id/]]></to>
5 <complete>1</complete>
</designer_url>
6 </rewrite>
7</global>
As we can see the code is quite self explanatory on how this works. One thing to note is magento uses
‘preg_replace’ to match url, so we can use regular expressions as well.