How to create redirects from old URL keys to new ones via Magento 2 import

- Magento 2, Our extensions

Magento 2 Development; Magento 2 tutorial

Chances are, you’ve been in a situation when the updated URL keys in Magento 2 continued returning a 404 error. Importing them via a CSV file introduced new URL keys but didn’t affect the exact user experience. Consequently, you had to edit URL keys manually per each store page. 

Below, we describe how to avoid this problem. You will learn how to create redirects from old URL keys to new ones using a CSV import in Magento 2. You will need an additional instrument to smooth the angles of the default Magento 2 import and get what you want from importing URL redirects to Magento 2. For more tips like this one, follow our Magento 2 Developer’s Cookbook. So, how to create redirects from old URL keys to new ones using a CSV import in Magento 2? Let’s see.

Importing new URL keys returns a 404 error

Aniket Singh writes about this problem. He has updated the ‘url_key’ column in a CSV file with new URL keys and imported it successfully. 

Although Aniket expected to get working redirects since the new URL keys were updated correctly, the old URLs were still returning a 404 error. 

In his situation, only manual updates of product URL keys straight in the Magento admin helped. Why did URL redirects work incorrectly after the CSV import? 

We may suppose that Aniket updated a CSV file with some mistakes. 

Magento 2 URL rewrites CSV explained

Magento 2 all product types import and export

To avoid issues when you import new URL keys to Magento 2, you need to follow a set of requirements. For instance, when you use the Improved Import & Export Magento 2 module, you need to specify request_path and target_path among other required attributes. 

The Request Path attribute of the URL rewrite indicates the path the visitor needs to hit to get redirected. Although the paths are specified in reference to the store name, they do not contain the full store name. As a result, store.com/path1 should look like /path1.

The Target Path attribute, in its turn, indicates the path the visitor will be redirected to when proceeding to request_path. And just like in the example above, your store.com/path1 should look like /path1.

So, if you want to replace yourstore.com/about with yourstore.com/about-us, you need to specify /about in the request_path column and /about-us in the target_path column.

Learn how to arrange your table with URL rewrites here: URL Rewrites Magento 2 sample import and export table and attributes explained.

How to create redirects from old URL keys to new ones using a CSV import in Magento 2

The Improved Import & Export Magento 2 extension lets you run the Magento 2 URL rewrites import as follows:

  • You can freely import all URL redirect types to your e-commerce website:
    • No,
    • Temporary (302 redirects),
    • Permanent (301 redirects);
  • The extension lets you update URL rewrites with new target paths;
  • Alternatively, you can use the import process to delete URL rewrites in bulk.

You’ve created a properly arranged CSV file with new URL keys. Now, it’s time to import it to Magento 2, creating redirects from old URL keys to new ones:

  1. Go to System -> Import Jobs;
  2. Create a new import job;

  3. Select URL Rewrites as your import entity;
  4. Configure other job settings.
  5. Import URL rewrites to Magento 2.

That’s it! Now, you can create redirects from old URL keys to new ones using a CSV import in Magento 2. In addition to that, the Improved Import & Export extension lets you import and export all core entities of Magento 2, create cron-based schedules to run automated transfers, and map third-party attributes to ones used in Magento. For instance, your CSV file contains old_path and new_path columns instead of request_path and target_path. Our extension lets you match old_path to request_path and new_path to target_path right in your admin. It means that you don’t need to replace the original file columns with the ones Magento 2 requires. 

Moreover, Improved Import & Export works with XML, XLSX, JSON, and other file formats in addition to CSV. It also supports various file sources, such as Google Sheets, Dropbox, Google Drive, etc. 

If standard file transfers are not what you’ve been looking for, the module provides the ability to run API-based import and export processes. Follow this link to learn more about the Improved Import & Export extension: 

Get Improved Import & Export Magento 2 Extension