>8 Swift Clicks to Create URL Redirect in Magento 2

8 Swift Clicks to Create URL Redirect in Magento 2

Magento 2 provides several SEO optimization options for your store. Redirection is the most commonly preferable way that developers would use. Redirection 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.

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.

Magento 2 Add 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

Click #8

Fill in Description if you want to give annotation.

give annotation on magento url redirect

Now you have created 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 redirect URL in Magento 2. You can create more URL redirect by I just instructed

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

Thanks!