How to set redirect url in Magento 2?

In Magento 2, You can redirect to any link or forcefully redirect to any link by Magento\Framework\Controller\Result\RedirectFactory object.

You can redirect from plugin or controller file to any link on the site by setPath() or setUrl() function.

Using setUrl($url) -> You need to pass link as parameter for redirect
Using setPath($path) -> You need to pass path(action URL) as parameter

/* Using Direct path, you need to pass action URL in a setPath() method */
For redirect to cart page, Pass action path as checkout/cart,
For Redirect to contact page,a parameter is contact
For Redirect to a login page, a parameter is customer/account/login

 

Pass Query string using above function by below methods,

You can redirect to any link by direct URL using below way,

You need to pass Direct URL for a redirect to a specific link in Magento 2.