Get Free Audit

How To Validate URL Rewrites in Magento 1 and Magento 2 (Magento Certificaton Exam)

Aug 29, 2017

5712 Denis Natalevich

How To Validate URL Rewrites in Magento 1 and Magento 2 (Magento Certificaton Exam)

Magento 1

Request Flow

  • app/Mage.php (Mage::app()->run())
  • app/code/core/Mage/Core/Model/App.php
  • Init and Dispatch controller $this->getFrontController()->dispatch();
  • app/code/core/Mage/Core/Controller/Varien/Front.php

When the front controller is dispatched, first off, there’s a check whether Url is the link to product or category. Everything starts from Mage_Core_Controller_Varien_Front->dispatch();


Call the rewrite method in Mage_Core_Model_Url_Rewrite_Request  in which the _rewriteDb() is called, then the method loadByRequestPath is called. Load rewrite information for request If $path is array — we must load possible records and choose one matching earlier record in array.


Request in the database in the method loadByRequestPath:

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


In case there’re Url rewrites in the base, set the redirect header:

Magento 2

In Magento 2 we have a separate router for processing URL rewrites, that’s why except rewrite mechanism, it’s necessary to understand Routing Mechanism. Each request is sent through the series of routers.

Flow of default routers:
Base Router → CMS Router → UrlRewrite Router → Default Router

Request Flow
index.php -> Bootstrap::create() -> create ObjectManagerFactory -> App::launch() -> FrontController::dispatch() -> router::match() -> Controller::execute()

Routers loop in FrontController::dispatch()

1. Base Router
Magento\Core\App\Router\Base
At base router standard Magento URL (front_name/action_path/action/param_1/etc_params/) is matched.

2. CMS Router
CMS Router is used for handling the CMS Pages and it also sets the module name to “cms”, controller name to “page” and action name to “view” so that makes the full path to execute is /vendor/magento/module-cms/Controller/Page/View.php controller

3. URL Rewrite Router
In Magento 2 UrlRewrite has its own router Magento\UrlRewrite\Controller\Router. using Url Finder to get URL rewrite that matches URL from the database:


In case if Url rewrite is found, it is redirected to requertPath found.

4. Default Router (404 Router)
Magento\Framework\App\Router\DefaultRouter

The last in the routers loop. It’s used when every other router doesn’t match. In Magento 2 we can create a custom handle for “Not Found” page to display custom content.

In such a manner, if Url put by the user is Url rewrite, so FrontController::dispatch() doesn’t find Url in Base route firstly, then it doesn’t find in cms route and when it gets URL Rewrite, Router calls the method Magento\UrlRewrite\Controller\Router::match() and using Url Finder to get URL rewrite that matches URL from the database and is redirected to request_path from the database received in url_rewrite table.

All in all, having worked with Magento 1 and Magento 2, you can see the difference in Url rewrites. In fact, it doesn’t look so profound, but there are some minor things that need consideration. Hope the article helps to cope with the issue.

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

Post a new comment

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