---
title: "How to Restrict Customer to Access CMS Page and Redirect to Login Page in Magento 2"
url: "https://magecomp.com/blog/how-to-restrict-customer-to-access-cms-page-and-redirect-to-login-page-in-magento-2/"
date: "2020-09-11T15:13:04+00:00"
modified: "2025-02-20T10:25:06+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 407
reading_time: "3 min read"
summary: "Hello Magento Friends ?,"
description: "You can restrict access to CMS Page for the users that have not registered and redirect them to the login page in Magento 2. Learn how you can implement it."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Magento 2: How to Add Custom Button in Minicart"
    url: "https://magecomp.com/blog/magento-2-add-custom-button-in-minicart/"
  - title: "How to get Product List in System Configuration in Magento 2"
    url: "https://magecomp.com/blog/get-product-list-system-configuration-magento-2/"
  - title: "All you need to know about Store Pickup Extension for Magento 2"
    url: "https://magecomp.com/blog/store-pickup-extension-for-magento-2/"
---

# How to Restrict Customer to Access CMS Page and Redirect to Login Page in Magento 2

_Published: September 11, 2020_  
_Author: Dhiren Vasoya_  

![How to Restrict Customer to Access CMS Page and Redirect to Login Page in Magento 2](https://magecomp.com/blog/wp-content/uploads/2020/09/How-to-Restrict-Customer-to-Access-CMS-Page-and-Redirect-to-Login-Page-in-Magento-2-1024x512.webp)

**Hello Magento Friends ?,**

Welcome to MageComp’s Technical Blog series. Today we are going to dig out How to Restrict Customer to Access CMS Page and Redirect to Login Page in Magento 2. Our previous blog was [**Solved: Export File gets “Message is added to queue, wait to get your file soon” in Magento 2.3.X**](https://magecomp.com/blog/solved-export-file-gets-message-is-added-to-queue-wait-to-get-your-file-soon-in-magento-2-3-x/)**.**

[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-3.webp)](https://magecomp.com/services/hire-magento-programmer/)

## Introduction:
You might have visited some sites that won’t allow you access to some page if you are not logged in. Magento 2 also facilitates to restrict users who are not registered. When the user seeks access to the CMS page of your Magento 2 store, you can redirect them to the login page. After the user login, it will be again redirected to the previous page.
Let’s see, How to Redirect Customer to Access CMS Page and Redirect to Login Page in Magento 2 ?
## Steps to Restrict Customer to Access CMS Page and Redirect to Login Page in Magento 2:
**Step 1:** First, we need to create an **“events.xml”** file inside the following path:
**app\code\Vendor\Extension\etc\frontend\events.xml**

```
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch_cms_page_view">
        <observer name="add_login_checker"
        instance="VendorExtensionObserverRestrictcmspage" />
    </event>
</config>
```

 **Step 2:** Lastly, create the **“Restrictcmspage.php”** file inside the following path:
**app\code\Vendor\Extension\Observer\Restrictcmspage.php**

```
<?php
namespace VendorExtensionObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkEventObserver;
use MagentoCustomerModelSession as CustomerSession;

class Restrictcmspage implements ObserverInterface
{

    protected $_actionFlag;

    public function __construct(
        CustomerSession $customerSession,
        MagentoFrameworkAppActionFlag $actionFlag,
        MagentoFrameworkAppResponseRedirectInterface $redirect
        )
    {
        $this->customerSession = $customerSession;
        $this->_actionFlag = $actionFlag;
        $this->redirect = $redirect;
    }
    public function execute(Observer $observer)
    {
        if (!$this->customerSession->authenticate())
       {

            $this->_actionFlag->set('', MagentoFrameworkAppActionAction::FLAG_NO_DISPATCH, true);
            if (!$this->customerSession->getBeforeUrl())
            {
                $this->customerSession->setBeforeUrl($this->redirect->getRefererUrl());
            }
        }
        return  $this;
    }
}
```

That’s it.
[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-2.webp)](https://magecomp.com/magento-malware-removal.html)

## Conclusion:
You have successfully implemented Restrict Customer to Access CMS Page and Redirect to Login Page in Magento 2 and you are free to customize this code according to your need for fetching data. If you have any queries write down in the comment section below. Help your Magento friends with this by sharing the article with them.
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2023/10/Magento-2-Speed-Performance-Optimization-Services-CTA-4.webp)](https://magecomp.com/magento-performance-optimization.html)

**Happy Restricting ?**


---

_View the original post at: [https://magecomp.com/blog/how-to-restrict-customer-to-access-cms-page-and-redirect-to-login-page-in-magento-2/](https://magecomp.com/blog/how-to-restrict-customer-to-access-cms-page-and-redirect-to-login-page-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-19 23:28:04 UTC_  
