cancel
Showing results for 
Search instead for 
Did you mean: 

i want to redirect my magento page from account/login to previous page like jens.html

i want to redirect my magento page from account/login to previous page like jens.html

i have a magento site in which whenever i go to some page in that site i am redirected to account/login page so is there any way to store that previous page when i am redirected to login page please help

1 REPLY 1

Re: i want to redirect my magento page from account/login to previous page like jens.html

Hello @miniOrange_inc 

 

To redirect the Magento 2 login page (account/login) back to the previous page after a successful login, 

Here’s a step-by-step approach:

 

  • Create a Custom Module
    • Create the necessary directory structure: app/code/Vendor/Custom/
    • Create the module registration file: app/code/Vendor/Custom/registration.php
    • Create the module configuration file: app/code/Vendor/RedirectLogin/etc/module.xml
    • Create the event observer to handle the login process: app/code/Vendor/Custom/etc/events.xml
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
          <event name="customer_login">
              <observer name="redirect_after_login" instance="Vendor\Custom\Observer\RedirectAfterLogin"/>
          </event>
      </config>
    • Create the observer class to handle the redirect: app/code/Vendor/Custom/Observer/RedirectAfterLogin.php
      namespace Vendor\Custom\Observer;
      
      use Magento\Framework\Event\Observer;
      use Magento\Framework\Event\ObserverInterface;
      use Magento\Framework\App\Response\RedirectInterface;
      use Magento\Framework\App\RequestInterface;
      
      class RedirectAfterLogin implements ObserverInterface
      {
          protected $redirect;
          protected $request;
      
      
          public function __construct(
              RedirectInterface $redirect,
              RequestInterface $request
          ) {
              $this->redirect = $redirect;
              $this->request = $request;
          }
      
      
          public function execute(Observer $observer)
          {
              $redirectUrl = $this->request->getParam('referer');
              if ($redirectUrl) {
                  $this->redirect->redirect($this->request->getControllerAction()->getResponse(), $redirectUrl);
              } else {
                  $this->redirect->redirect($this->request->getControllerAction()->getResponse(), '/');
              }
          }
      }
       
    • Run the following commands to enable and set up the module:
      php bin/magento module:enable Vendor_RedirectLogin
      php bin/magento setup:upgrade
      php bin/magento cache:clean
      php bin/magento cache:flush

Hope it helps !

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

 

 

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9