In Magento, sometimes you need to change (rewrite) an existing URL into a new one. The reason may be:
- The old URL is not user-friendly
- There’s a typo in the old URL
- You have duplicate content issues
In those cases, permanently redirect the old URL into a new one is necessary to make your site more well-structured and perform good in SEO as well as look good in customers’ eyes. This guide is gonna walk you through how to create URL redirect in Magento 1 & 2 via Magento backend and using functions.
Create URL redirect in Magento backend
Magento 1
- In your Magento backend, go to Catalog > URL Rewrite Management.
- Click Add URL Rewrite.
- Magento allows you to create URL rewrite for products, categories, and custom URLs. Select which type of URL you want to rewrite from the dropdown list.
- If you select For category or For product, you will need to select a specific category or product whose URL you want to redirect.
- The URL Rewrite Information fields will show up for you to fill in.
- Type: this is Custom by default.
- ID Path: Enter the same as Target Path. (This will be automatically filled in if you choose For category or For product at previous step)
- Request Path: Enter the URL of the page to be redirected (the old URL), without the domain name. For example, if you want to redirect the URL https://www.example.com/test/old.html, you only need to enter “test/old.html”.
- Target Path: Enter the URL of the destination page (the new URL), without the domain name. For example, if you want to redirect the URL https://www.example.com/old.html into https://www.example.com/test/new.html, you need to enter “test/new.html”. (This will be automatically filled in if you choose For category or For product at previous step)
- Redirect: Select Permanent (301). Unless you really only want to redirect a URL for a short period of time, you should always choose 301 redirect. It’s good for SEO.
- Description: You can note down the purpose of the rewrite.
- When you’ve done, click Save.
- Go to System > Index Management. Check the box at Catalog URL Rewrites. Set the Actions control to “Reindex Data,” and click the Submit button.
Magento 2
- In your Magento 2 backend, go to Marketing > SEO & Search > URL Rewrites.
- Click Add URL Rewrite.
- Magento allows you to create URL rewrite for products, categories, CMS pages, and custom URLs. Select which type of URL you want to rewrite from the dropdown list.
- If you select For category or For product or For CMS page, you will need to select a specific category or product or CMS page whose URL you want to redirect.
- The URL Rewrite Information fields will show up for you to fill in.
- Store: Select the specific view where the rewrite applies.
- Request Path: Enter the URL of the page to be redirected (the old URL), without the domain name. For example, if you want to redirect the URL https://www.example.com/test/old.html, you only need to enter “test/old.html”.
- Target Path: Enter the URL of the destination page (the new URL), without the domain name. For example, if you want to redirect the URL https://www.example.com/old.html into https://www.example.com/test/new.html, you need to enter “test/new.html”. (This will be automatically filled in if you choose For category or For product or For CMS page at previous step)
- Redirect Type: Select Permanent (301). Unless you really only want to redirect a URL for a short period of time, you should always choose 301 redirect. It’s good for SEO.
- Description: You can note down the purpose of the rewrite.
- When you’ve done, click Save.
Create URL redirect using Magento functions
The redirect functions are available in Mage_Core_Controller_Varien_Action class.
/* Redirect to certain url */
_redirectUrl($url)
/* Redirect to certain path */
_redirect($path, $arguments=array())
/* Redirect to success page */
_redirectSuccess($defaultUrl)
/* Redirect to error page */
_redirectError($defaultUrl)
/* Set referer url for redirect in response */
_redirectReferer($defaultUrl=null)
/* Identify referer url via all accepted methods (HTTP_REFERER, regular or base64-encoded request param) */
_getRefererUrl()
/* Check url to be used as internal */
_isUrlInternal($url)
Examples:
Use the redirect functions in your controller class
$this->_redirect($path, $arguments=array());
// OR,
$this->_redirectUrl($url);
or
$url = “http://example.com”;
Mage::app()->getFrontController()->getResponse()->setRedirect($url);
Redirect with path and arguments
Mage::app()->getFrontController()
->getResponse()
->setRedirect(Mage::getUrl($path, $arguments));
Redirect from Model or Observer
$url = “http://example.com”; // e.g. Mage::getUrl(‘customer/account/login’)
Mage::app()->getFrontController()
->getResponse()
->setRedirect($url);
Related posts:
How to Change Base URLs in Magento 1 & 2
Why and How to add Canonical URL Magento instead of 301s or 302s?
A very informative article, enabling URL rewrite’s really contributes in improving search engine optimization of Magento store, there’s another really good tutorial for this magenticians. I found it really helpful and to the point, do have a look at it too. Thanks