I want to redirect to Magento Pages (Product/Categories etc.) only if Customer is Logged in. Otherwise it Should Redirect to Customer Login page

I want to redirect to Magento Pages (Product/Categories etc.) only if Customer is Logged in. Otherwise it Should Redirect to Customer Login page

October 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
I want to redirect to Magento Pages (Product/Categories etc.) only if Customer is Logged in. Otherwise it Should Redirect to Customer Login page

Redirecting Magento Pages Based on Customer Login Status

In this article, we will address an issue you’re facing with Magento 2 redirecting to a white screen when accessing category pages while not logged in. Let’s dive into the problem and find a solution.

Understanding the Current Setup

Your current observer setup is checking for login status before each controller action using the controller_action_predispatch event. If the user isn’t logged in, it redirects them to the customer account login page. This works perfectly when you hit the homepage or certain controller actions like ‘login’.

However, the issue arises when you try to access a category page (like “http://magento2.test/men/tops-men/tees-men.html”). In this case, Magento tries to handle the request before your observer can intervene, leading to a 502 Bad Gateway error.

The Problem with Your Current Approach

The main problem lies in the sequence of events. When you access a category page, Magento doesn’t dispatch the controller_action_predispatch event until after it has already tried to process the request. By that point, if the user isn’t logged in and hasn’t been redirected yet, Magento throws a 404 error or falls back to the default 502 response.

A Better Approach: Using Controller Plugins

To solve this issue, we should use controller plugins instead of observers for redirection. With a controller plugin, you can intercept the request before it reaches the controller action and handle the redirection there.

Let’s see how to implement this:

  1. Create a new file in your module: Vendor/Module/etc/frontend/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\Action\AbstractAction">
        <plugin name="checklogin"
                type="Vendor\Module\Plugin\Controller\CheckLogin"
                sortOrder="100"/>
    </type>
</config>
  1. Create the plugin class: Vendor/Module/Plugin/Controller/CheckLogin.php
namespace Vendor\Module\Plugin\Controller;

class CheckLogin
{
    protected $_customerSession;
    protected $urlInterface;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\UrlInterface $urlInterface
    ) {
        $this->_customerSession = $customerSession;
        $this->urlInterface = $urlInterface;
    }

    public function beforeExecute(\Magento\Framework\App\Request\Http $request)
    {
        if (!$this->_customerSession->isLoggedIn()) {
            $url = $this->urlInterface->getUrl('customer/account/login');
            $request->setForcedRedirect($url);
        }
    }
}

How This Solution Works

The beforeExecute method of the plugin is triggered before Magento processes any controller action. In this early stage, we can check if the customer is logged in and redirect them to the login page if necessary.

Using setForcedRedirect, we ensure that the redirection happens immediately and before any other processing occurs, preventing Magento from throwing errors due to missing controllers or actions.

Conclusion

In conclusion, when dealing with redirections based on user authentication status in Magento 2, it’s crucial to use controller plugins instead of observers. This approach ensures that the redirection logic is executed early enough to prevent errors and provide a seamless customer experience.

Remember, always refer to the official Magento Developer Documentation for more detailed information on Magento 2 development practices.

Try implementing this solution in your project and let me know if you have any further questions. Happy coding!

Magento 2 Catalog Cache Security

Our network