I am using below function to send 403 status and "access denied" content and redirect user to some other website or custom page if user's IP is not in list of allowed IP's (whitelist)
protected function denyAccess($observer)
{
/** @var \Magento\Framework\App\Action\Action $action */
$action = $observer->getControllerAction();
/** @var \Magento\Framework\App\Response\Http $response */
$response = $action->getResponse();
$response->clearBody()->setStatusCode(\Magento\Framework\App\Response\Http::STATUS_CODE_403);
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$redirectUrl = 'https://someotherwebsitestore.com';
header("Location:".$redirectUrl);
exit;
}
- How do I redirect user from observer to some other page or website and keep on same website if IP is whitelisted?
- Using
header
when IP is not allowed, the user still remains on website. Only when cache is cleared, user gets redirected?