Url Rewrites in Magento 1 and Magento 2 (Developer Certification Exam)

Url Rewrites in Magento 1 and Magento 2 (Developer Certification Exam)

Magento allows having “pretty” addresses. It means that instead of the link /catalog/category/view/id/4, we can get /women.html. Such type of links is clear for users and easy to remember. Moreover, it can improve search engine indexing and ranking.

When we create a category or a product, Magento generates SEF Url automatically. Then we can get into category/product editing and set our own SEF Url. Magento has its own Url rewrite manager where we can observe and change all existing SEF Urls into products as well as add custom Url rewrites.

Let’s compare Magento 1 and 2 and define how Url rewrites work. After Magento 1 or 2 installation with simple data, we can see “pretty” links.

Category/product links

Magento 1:
Category: /index.php/women.html; /index.php/women/new-arrivals.html
Product: /index.php/women/new-arrivals/tori-tank-469.html

Magento 2:
Category: /index.php/women.html; /index.php/women/tops-women.html
Product: /index.php/mona-pullover-hoodlie.html

As we can see, there’s nothing irregular and Urls are usual. Now product Urls in Magento 2 do not have a category name. You can change it by getting in Store -> Configuration -> Catalog -> Catalog -> Search Engine Optimization -> “Use Categories Path for Product URLs” set to “Yes”. After that Reindex using php bin/magento indexer:reindex. It’s possible to remove index.php from Urls and, as a result, Urls will be simpler and prettier.

Remove index.php from Urls

Magento 1:
To enable SEF URLs on Magento 1, go to your Magento admin panel -> System tab -> Configuration -> General section -> Web. Then expand the Search Engines Optimization section and change the value of Use Web Server Rewrites to Yes and save the changes.

Magento 2:
For Magento 2, go to the Magento admin panel -> Stores -> Configuration -> General section -> Web. Expand the ‘Search Engine Optimization’ section and change the value of the Use Web Server Rewrites option to Yes and save the changes.

SEF Urls are really good, but where can they lead?

Url structure

Url structure in Magento 1 and Magento 2 is the same. 

{base_url}/{front_name}/{controller}/{action}.

Now when entering /women.html,  /catalog/category/view/id/4 is called, where catalog – front_name, category – controller, view – action and param &id=4. How did we manage to get /catalog/category/view/id/4  from  /women.html. We managed it with the help of Url rewrite mechanism.

Where can URL rewrites be observed from admin panel?

Magento 1:
Catalog > URL Rewrite Management

Magento 2:
Marketing > SEO & Search > URL Rewrites

In both Magento versions, we can see Request Path column including “pretty” URL and Target Path column containing a real URL path of the definite page. Let’s consider Target Path for categories and products.

Magento 1:
Category: /catalog/category/view/id/{category_id}
Product: /catalog/product/view/id/{product_id}

Magento 2:
Category: /catalog/category/view/id/{category_id}
Product: /catalog/product/view/id/{product_id}

Here is everything as before. The links to products and goods are the same. From admin’s point of view, nothing has changed. But it should be mentioned that if we look at the base, there are some changes appeared and Url rewrites are stored in a different way.

Where can Url rewrites be observed in the base?

Magento 1:
core_url_rewrite

Magento 2:
catalog_url_rewrite_product_category
url_rewrite

Now there are two tables, in url_rewrite table, there’re columns url_rewrite_id, category_id, product_id which are separated. The most significant point is that in Magento 2 in url_rewrite table there are not only links to categories and goods, but cms page. In Magento 1 it was stored in cms_page table.



Post a new comment

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