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();
    $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);

    $redirectUrl = 'https://someotherwebsitestore.com';
  • 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?
  • Is the above code from some class? I mean is it internal of magento or outside?
    – Avesh Naik
    Commented Aug 23, 2018 at 18:32
  • Is the Magento 2 code only. Commented Aug 23, 2018 at 18:33

1 Answer 1

protected $resultRedirect;

 public function __construct(\Magento\Framework\Controller\ResultFactory $result){
     $this->resultRedirect = $result;

protected function denyAccess($observer)
   $resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);

   return $resultRedirect;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.