Get Free Audit

URL Rewrites. Part I (Magento Certified Developer Exam)

Dec 10, 2012

907 Aleksander Tretjak

URL Rewrites. Part I (Magento Certified Developer Exam)

Overall structure of an address and its elements is well represented in the first comment to the app/code/core/Mage/Core/Model/Url.php model. You can refresh your knowledge there.

Apache mod_rewrite module plays an important role in URL rewriting. Rules, specified in .htaccess, will not be processed without it. In order to activate URL rewriting, .htaccess.sample file should be renamed .htaccess. The following part is responsible for rewriting:


Moreover, the following parameter should be activated in the Admin Panel:

Web -> Search Engines Optimization -> Use Web Server Rewrite

Basic script will thereby be excluded from an address line. URL rewriting will work after these changes.

In Front controller article dispatch() method was described; let’s take a look at the following part of this method:


URL rewriting takes place here. There are two URL rewriting types in Magento:

  1. URL rewriting with database
  2. URL rewriting via configuration files

Let’s take a look at the first type of URL rewriting. It allows rewriting with the help of Mage_Core_Model_Url model, according to the rules stored in the database. They can be created in the admin interface: Catalog->URL Rewrite Management. Two variants are present there:

  • System
    • Category
    • Product
  • Custom

System – URLs are created by system on the assumption of the data entered in the particular case. Administrators can also create their own addresses.

Custom – URLs are created by administrators. It can also be specified here if the link redirects to a different site (301 or 302). More will be said later on.

Let’s look more closely at the structure of the table where URL rewriting data is stored.

URL Rewrites

Table content tells us about fields purposes for each type.

 

System (Category)

URL Rewrites

 

Custom (Product)

URL Rewrites

 

Custom (Product)

URL Rewrites

 

Custom (Category)

URL Rewrites

 

Options field is considered below in details.

Let’s return to dispatch() method. First, it’s figured out if address rewriting is necessary:


Next, Mage_Core_Model_Resource_Url_Rewrite object is created and its rewrites() method is called. The method searches and rewrites addresses. First, validation is performed:


Next, different variants of a current path, used for database search for this request approval, are prepared.


Addresses can have different structure but the same sense. That is what taken into account here.

  • furniture/
  • furniture

Attention should also be drawn to the address of the following kind:


They have a high priority and are processed in the first place, because they get into the array first. Next, Mage_Core_Model_Resource_Url_Rewrite resource with its loadByRequestPath: method comes in.


Here is where the following request is prepared and executed:


Search depends on the admin store and “current” store. All the found lines are selected from the database and requested path correspondence search is triggered.


Let’s next return to Mage_Core_Model_Url_Rewrite:

Vlad Yunusov
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Vlad


If no matches are found (i.e. database search did not have any results and there is a $_GET[‘___from_store’] – code of another store), we look for matches taking the store into account.


There are two redirect types in Magento

301 redirect – RP

302 redirect – P

302 redirect is being used by default.


$external = substr($this->getTargetPath(), 0, 6); – selecting the protocol, if there is one.

Checking what redirect type exactly we will execute:


Checking if the target address is external, if yes – the redirect is executed:


If it’s not external and one of redirect types is detected, we’ll do the redirection to the target address, taking into account the specified redirect type.


This is a Custom rules types processing, created by system or administrator. This mechanism can be applied to the scenarios of changing the category or product URL Key while moving the product from one category to another. In this situation, when you save, RP redirection to the old address is created.  Custom record with the generated path_id will be created.

app/code/core/Mage/Catalog/Model/Url.php


Of the following kind:

URL Rewrites

For this rewriting type, RP is selected for options by default.

This mechanism is used in the indexation. We’ll return to it later.

If we specify an “unusual” store by default, target address will be created considering the store:


That ends the URL rewriting.

Before we focus on the second rewriting type, let’s take a look at indexation process for the addresses.

This functionality can be found here: System->Index Management->Catalog URL Rewrites.

Indexation module is defined in the index config.xml section of the catalog module:


Addresses start to be processed in the following module:

app/code/core/Mage/Catalog/Model/Indexer/Url.php


Generation mechanism is located here:

app/code/core/Mage/Catalog/Model/Url.php


Let’s take a look at the main stages of the work of this mechanism:

1. If store is not specified, the following is executed for all the existing ones:


2. All the old rewriting rules for category and products are found and deleted:


3. Rewriting rules for categories and products are updated:


4. Unused rewriting rules for products (when the product is moved to another category) are found and deleted:


These are the main stages of addresses rewriting with the help of Magento database rules. The next part of the article will be devoted to URL rewrite with rules specified in configuration files.

Vlad Yunusov
Partner With Us Looking for a partner that will help you to grow your business? We are the right company to develop your webstore. Feel free to get in touch with us. We will be happy to discuss your business opportunities and provide you with a Free Quote. Talk to Vlad

3 Comments

  1. Dinoop,

    1. You can redirect from one URL to another one by using URL Rewrite Management in Magento or in .htaccess file.

    2. You need to create route processing for that specific module and there you specify the address of the required template.

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *