Here, we will be looking at how to set up both manual and automatic URL rewrites in Magento 2, but before we do that, we need to understand:
What’s a URL rewrite
A rewrite is where a page URL key is changed to a new page key, with a reference set up, so that anyone using the old URL page key is sent to the new one. These are used so that search engines/people end up on the page they’re looking, so they don’t end up on a ‘404/missing page’.
Let’s look at an example:
One of the products on your website is a greeting card. Due to a human error, there was a mistake when it was set up, so now you have a misspelling on the product URL –
You go in to change to URL, to correct this mistake so now your product URL is –
However, one of your blogs the points to the products old (misspelt) URL, which doesn’t get changed, now if anyone clicks on the link to the product, they’ll end up on a ‘404 page’, leading them to believe the product no longer exists.
A URL rewrite fixes this issue so that wherever there is a link to the old URL, there is a reference in your website for that to take them to the new updated URL.
Why they’re important
These are needed for both user experience and search engines.
For a user the result would be that they follow a link that leads them to a ‘404 does not exist page’, this would not be a great user experience and could result in a lost sale. Having a redirect in place lets users get to the page they wanted to visit.
For search engines, they will follow this link and find that the page does not exist, it will believe the page has been removed from the website, any authority this may have will be lost, and if the page got traffic, the search engines will remove this from the search results as it’s not good for users to land on a page that does not exist. A 301 redirect lets them know that the page has been moved to a different URL key and that it still exists on the website.
In Magento, this all can be taken care of by the CMS itself. Magento allows for these rewrites to be implemented both automatically and manually. Automatic URL rewrites will be put in place by the system whenever you change a category, product and CMS page URL key.
Manual are redirects are ones that you set up yourself.
Turning on automatic URL rewrites in Magento
- Log into your Magento admin area
- Head to Store > Configuration > Catalog > Catalog
- Expand the Search Engine Optimisation tab.
- Change Create Permanent Redirect for URLs if URL Key Changed to yes.
- Hit the save button and you’re finished.
Log into your Magento backend and head to Store > Configuration.
In the configuration menu, you’ll need to expand the Catalog section, then go to the Catalog sub-menu item.
Now you’ll need to look down the page and expand the Search Engine Optimisation. In here there will be an option called Create Permanent Redirect for URLs if URL Key Changed, make sure this is set to yes.
Hit the save button and you’re done, you may also need to flush the cache for this to take effect). This will now ensure going forward that a URL key for a product, category or CMS page, the old URL key will point to the new one.
Setting up a manual URL rewrite in Magento
- Log into your Magento admin area
- In the left menu head to Marketing > URL Rewrites
- Click the Add URL Rewrite button
- In Request Path, put the URL key you want to be changed
- In Target Path put the final page URL key
- Choose your redirect Type – 301 (permanent) or 302 (temporary)
- Add a description of why this is being set up
- Save and test it works correctly
What are 301 and 302 redirects?
You may have seen in the overview of adding a manual redirect in Magento, that there was an option of choosing either a 301 (permanent) or 302 (temporary) redirect. If you’re not sure what the difference is and which one you should use, don’t worry, we’ll briefly explain.
301 (permanent) redirect – this is used where you want to change a page’s URL key to a new URL key permanently. If you have a product page (like above), where there is a spelling error in the URL key, which you want to correct. In this case, you’d want this to be a permanent change.
302 (temporary) redirect – this is used where you want to point a page’s URL key to another page, but only temporarily, where the page URL key will be changed back in future. This could be used where you have a page for events which needs to be updated. You set up a temporary page, explaining that the events page is being updated and for all the information about upcoming events to visit your Facebook page. In this case, you’d set up a 302 redirect from the events page, to the temporary page. This then lets search engines know that, though this page is temporarily unavailable and will be back soon.
Now, on how to implement a URL rewrite in Magento 2.
Log into your Magento admin area and from the left menu, head to Marketing > URL Rewrites.
You’ll be taken to a page which has all the URL rewrites that have been created. Click the Add URL Rewrite button.
Make sure the Create URL rewrite drop down at the top is on Custom, select the store you want the rewrite applied to. In the Request Path, put the URL key you want pointing to a new page. In Target Path, put the final URL key where you want people to end up.
For Redirect Type, select either 301 (permanent) or 302 (temporary).
Add a brief description as to why this is being set up and hit save to finish.
Head to the front end of the website to test that the request path URL key takes you to the target path URL key.
Now you’ve know what URL rewrites in Magneto are and how tos et them up.