Home » Magento® Tutorial » Magento® Tutorials » Lesson 30: URL Rewrite in Magento (part 2)
Lesson 30: URL Rewrite in Magento (part 2)

Lesson 30: URL Rewrite in Magento (part 2)

Hello guys! Last week we finished part 1 of URL Rewrite in Magento. In the second part of this lesson, we will go in details of how to rewrite URL in Magento. Let’s get started!

Lesson 30: URL Rewrite in Magento (part 1)

4. How to rewrite URL in Magento

4.1. Create URL rewrite manually

Login to Admin of Magento and click Catalog menu > URL Rewrite Management

URL rewrite

Click on button “Add URL Rewrite” to create a new URL rewrite

URL-rewrite-2

Choose URL rewrite for category, product or custom.

URL-rewrite-3

  • id_path serves as an identifier to the rewrite entry and is unique within a store. Its value is comprised of ‘category’ or ‘product’ and the ID of the current category or product, e.g. “category/10” or “product/99”. Products can have multiple rewrites, each per category it is assigned to; and in such cases id_path can be extended with a category id, e.g. “product/99/10”. The id_path property plays a part when a category or a product URL is created – the system generates an id_path and looks into the rewrite table for a matching entry, fetches the request_path and produces a URL.
  • target_path – this property looks like a non-descriptive URL we discussed above. It contains references to the module, controller, action, and entity ID. When dispatching a request, Magento uses the request_path extracted from the request URL to match to a target_path. The matched entry will be used to pass the request to the proper controller action which will render the output.
  • request_path is a descriptive URL slug generated from the entity’s urlkeyattribute. A request_path of a category contains the urlkey of the parent categories, e.g.: furniture/living-room.html for the “living-room” category. Note the ”.html” suffix, which you can control with the configuration settings. For products Magento generates multiple entries with different request_paths. First, a rewrite with a simple request_path consisting only of the urlkey of the product itself, like ottoman.html. Then for every parent directory a category path is added, e.g. furniture/living-room/ottoman.html. The request_path property of sub-categories include a complete path, e.g. furniture/living-room.html.
  • store_id – in a multistore environment there can be multiple rewrites for the same category or product reflecting their possibly different names (internationalization) or category membership.
  • is_system – this property indicates that the rewrite has been created by the system automatically and can be modified when URL rewrites re-indexing starts. Permanent redirects created automatically by the shop when a product or a category urlkey is changed are set to non-system (i.e. is_system = 0) so that they can’t be affected by the URL re-indexing.
  • options contains a redirect code for the redirect rewrites. Regular URL rewrite entries have nothing in this property – they do not make web clients redirect. But as we have mentioned above, Magento can create redirect rewrites when necessary. These entries will have “PR” (“permanent redirect”) code saved in this property. Shop admins can create custom rewrites and provide redirect options for them. Custom rewrites can be temporary redirects, their options property will have “R” (“redirect”). We will discuss redirect options in details further below.

After filling in all fields, click “Save” to create a new URL rewrite.

4.2. Rewrite controller

For example: Rewrite controller checkout/cart in module Checkout

Code: app\code\local\Magestore\Lesson30\etc\config.xml

Step 1: Add configure in config.xml file

  • Method 1: insert config rewrite into tag global file config.xml
<global>

<rewrite>
<lesson30_checkout_cart>
<from><![CDATA[#^/checkout/cart/#]]></from>
<to>/lesson30/checkout_cart/</to>
</lesson30_checkout_cart>
</rewrite>
...
</global>

from: expression to controller link

to: controller to rewrite

  • Method 2: Insert config routers into tag frontend file config.xml
<frontend>

<routers>
<checkout>
<args>
<modules>
<Magestore_Lesson30 before="Mage_Checkout">Magestore_Lesson30</Magestore_Lesson30>
</modules>
</args>
</checkout>
</routers>
...
</frontend>
  • Method 3: Insert config router into tag global to rewrite all actions or one action of controller

Rewrite all action: rewrite all actions in controller

<global>

<routers>
<checkout>
<rewrite>
<cart>
<to>lesson30/cart</to>
</cart>
</rewrite>
</checkout>
</routers>
...
</global>

Rewrite each action: Rewrite an identified action in controller

<global>

<routers>
<checkout>
<rewrite>
<cart>
<actions>
<index>
<to>lesson30/cart/index</to>
</index>
</actions>
</cart>
</rewrite>
</checkout>
</routers>
...
</global>

Step 2: Write controller file

In this step, we need to write a controller (from controller rewrited), then rewrite actions we want.

Code: app\code\local\Magestore\Lesson30\controllers\CartController.php 

<?php

/**
* Magestore
*
* Online Magento Course
*
*/

/**
* Lesson30 Index Controller
*
* @category Magestore
* @package Magestore_Lesson30
* @author Magestore Developer
*/
require_once 'Mage/Checkout/controllers/CartController.php';
class Magestore_Lesson30_CartController extends Mage_Checkout_CartController
{
public function indexAction() {
# Just to make sure
error_log('Yes, I did it!');
parent::indexAction();
}
}

 An important key to note is that Magento does not autoload controllers like it does with blocks and models. So, we’ll need to use “require_once” the file of the controller that we want to override.

You also may concern:


Ready to foster your knowledge with MAGENTO 2 BASIC GUIDE

The new Order Success combo with Magento 2 Inventory Management, Magento 2 Order Fulfillment, Magento 2 Dropship, Magento 2 Barcode Management and Magento 2 Purchase Management

magento 2 basic guide series


The following two tabs change content below.

Blanka

This is our talented developer who has been developing many outstanding extensions. If you meet Blanka in person, you will never forget his special laugh, very cheerful and relaxing.

Leave a Reply

Your email address will not be published. Required fields are marked *

*