Magento 2 create URL redirect

8 Swift Clicks to Create Magento 2 URL Redirect

by admin

Magento 2 provides several SEO optimization options for your store. Redirection is the most commonly preferable way that developers would use. Magento 2 URL Redirect means forwarding users from one URL to another. When the rule takes effect, any links pointing to the targeted URL will be diverted. URL redirect also make your current URLs more friendly to search engine tools.

Find out in this article how to redirect a custom URL to the targeted URL swiftly using Admin Panel, and how programmatically helps to create the point.

But first.

How many type of redirects are there?

Click #1

Log on to Admin Panel, navigate to URL Rewrites by Marketing > SEO & Search  magento 2 url rewrite admin panel

Click #2

Click Add URL Rewrite button.

add-magento-2-url-redirect

Click #3

Select Custom from the drop-down list of the Add New URL Rewrite.

Magento url rewrite

Select either the category/ product or CMS page to assign URL to that accordingly items.

Click #4

Select your default store view from the drop-down in Store view field.

Set store view Magento URL rewrite

Click #5 – 6

In the Request Path field, enter the Requested URL. (Requested URL is the previous URL which will redirect to the targeted URL). In the Target Path field, enter the Targeted URL which will serve as the target of the redirect rule for Requested URL.

create path for magento 2 url redirect

 

Click #7

Select either Temporary (302) or Permanent (301) in the Redirect Type using the drop-down list.

301 redirect magento 2

Read More: Simplest Tutorial On Creating Magento 301 Redirect Map For Your Store.

Click #8

Fill in Description if you want to give annotation.

magento-2-url-redirect-annotation

Now you have created Magento 2 URL redirect successfully within 8 instant clicks. Apart from implementing with Admin Panel, you might also like to know the following section on how to make Magento 2 URL Rewrite programmatically.

Programmatically redirect to the previous page

To redirect to the previous page from my custom action in Magento 2, use the code below for your controller :

namespace Bss\UrlRedirect\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
class Action  extends \Magento\Framework\App\Action\Action
{
public function __construct( ResultFactory $resultFactory)
{
$this->resultFactory = $resultFactory;
}
     public function execute ()
     {
          $ resultRedirect = $ this-> resultFactory-> create (ResultFactory :: TYPE_REDIRECT);
          $ resultRedirect-> setUrl ($ this -> _ redirect-> getRefererUrl ());
          return $ resultRedirect;
     }
}

In which:

  • Bss\UrlRedirect\Controller\Index is vendor\module\Controller folder\ Index folder.
  • At first, creating instance of ResultFactory by calling create(). Using that, we are redirected to another URL using setURL() method.
  • In the setUrl() method, we set desired URL. To redirect the action to previous page, set $this->_redirect->getRefererUrl() as parameter.

Programmatically redirect to any other URLs

To redirect to any other URL from my custom action in Magento 2, it is similarly to the above code but there is a adjustment:

  • Use $resultRedirect->setPath(‘www.example.com/xyz’); instead of $resultRedirect->setUrl($this->_redirect->getRefererUrl()) this will direct to the url we set.
namespace Bss\UrlRedirect\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
class Action  extends \Magento\Framework\App\Action\Action
{
public function __construct( ResultFactory $resultFactory)
{
$this->resultFactory = $resultFactory;
}
    public function execute ()
{
          $resultRedirect = $this->resultFactory->create(ResultFactory ::TYPE_REDIRECT);
         $resultRedirect->setPath ('www.example.com/xyz');
              return $resultRedirect;
    }
}

So I just finished instructions on how to create a Magento 2 URL redirect. You can create more URL redirect by I just instructed.

 

If you find this blog post useful for you please leave a comment.

 

Thanks!

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.