I'll tell you two ways to redirect from the observer.
1)
$controller = $observer->getControllerAction();
$this->redirect->redirect($controller->getResponse(), 'customer/account/login');
$controller = $observer->getControllerAction();
$url = $this->getUrl('customer/account/login'); //Magento\Framework\UrlInterface $url
$controller->getResponse()->setRedirect($url);
Example
<?php
namespace Magento\Captcha\Observer;
use Magento\Framework\Event\ObserverInterface;
class CheckForgotpasswordObserver implements ObserverInterface
{
protected $redirect;
public function __construct(
\Magento\Framework\App\Response\RedirectInterface $redirect
) {
$this->redirect = $redirect;
}
/**
* Redirect to login page
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var \Magento\Framework\App\Action\Action $controller */
$controller = $observer->getControllerAction();
$this->redirect->redirect($controller->getResponse(), 'customer/account/login');
return $this;
}
}
Checked this one,
protected $actionFlag;
protected $redirect;
public function __construct(
\Magento\Framework\App\ActionFlag $actionFlag,
\Magento\Framework\App\Response\RedirectInterface $redirect
) {
$this->actionFlag = $actionFlag;
$this->redirect = $redirect;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->redirect->redirect($controller->getResponse(), '*/*/edit');
}
This will work for you,
Happy Coding