Magento Tutorials

How to Create URL Redirect in Magento 1 & 2

create-url-redirect-magento

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

  1. In your Magento backend, go to Catalog > URL Rewrite Management.
  2. Click Add URL Rewrite.
  3. 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.
  4. If you select For category or For product, you will need to select a specific category or product whose URL you want to redirect.
  5. The URL Rewrite Information fields will show up for you to fill in.
    Magento URL Rewrite

    • 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.
  6. When you’ve done, click Save.
  7. 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

  1. In your Magento 2 backend, go to Marketing > SEO & Search > URL Rewrites.
  2. Click Add URL Rewrite.
  3. 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.
  4. 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.
  5. The URL Rewrite Information fields will show up for you to fill in.
    Magento 2 URL Rewrite

    • 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.
  6. When you’ve done, click Save.

Create URL redirect using Magento functions

The redirect functions are available in Mage_Core_Controller_Varien_Action class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 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

1
2
3
$this->_redirect($path, $arguments=array());
// OR,
$this->_redirectUrl($url);

or

1
2
$url = "http://example.com";
Mage::app()->getFrontController()->getResponse()->setRedirect($url);

Redirect with path and arguments

1
2
3
Mage::app()->getFrontController()
 ->getResponse()
 ->setRedirect(Mage::getUrl($path, $arguments));

Redirect from Model or Observer

1
2
3
4
$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?

How to Create URL Redirect in Magento 1 & 2
5 (100%) 3 votes
1 Comments

Dom

A knowledge craver who always strive to be wiser everyday.

    1 Comments

    1. Damon Moats
      11/15/2018 at 1:26 pm

      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

    Reply your comment

    Your email address will not be published. Required fields are marked*