1

How to redirect customers to custom page when they go to any other page.

I have integrated OTP verification, I want to redirect customers to the verification page if they are not verified.

1

Take the following steps to write a redirect rule for your Magento 2 website:

  1. Log in to your Magento 2 administrative panel.
  2. In the main menu, click Marketing
  3. In the Marketing menu, click URL Rewrites
  4. If you are familiar with Magento 1.x, Magento 2 uses the same interface from this point forward. Click Add URL Rewrite
  5. Leave the Create URL Rewrite drop-down list on its default setting, Custom. Fill the Store, Request Path, and Target Path fields .

The Request Path is the original path.

The Target Path is the new path corresponding to the new web address. This will serve as the target of the redirect rule.

In the Redirect Type drop-down list, select either 301 or 302, as appropriate

enter image description here

Click Save. If successful, you will see the message, “The URL Rewrite has been saved.”

If you want to redirect via Controller Then Refer to this question

  • Thanks for your information. I integrated otp verification in magento 2. I need users to redirect to verification page if they were not verified. – Ramesh KR Nov 24 '17 at 6:18
  • you can store value is customer session if he is not verified and Create plugin for Customer login and you can redirect form there – Vishwas Bhatnagar Nov 24 '17 at 6:33
  • 1
    @RameshKR Please Update This case in Question so you can get exact answer – Vishwas Bhatnagar Nov 24 '17 at 6:42
0

You can check you redirect condition before every action using observer, you have to use controller_action_predispatch event for this, check below steps

create app/code/Namespace/Module/etc/frontend/events.xml with below code

<?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">
      <observer name="my_observer" instance="Namespace\Module\Observer\MyClass" shared="false" />
  </event>
</config>

Then create you observer at file with below code app/code/Namespace/Module/Observer/MyClass.php

<?php 

namespace Namespace\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class MyClass implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
   {
       // your redirect condition code here
   }

}

reference

  • Don't you think "controller_action_predispatch" will be overkill , Why check if user is not logged in . Adding a plugin after login or registration Will do a prefect job – Vishwas Bhatnagar Nov 24 '17 at 6:36
  • @VishwasBhatnagar I suggested this because OP has asked to check before going to any page – Piyush Nov 24 '17 at 6:37
  • Ok, Please check his comments above . He has mentioned the case you can update your answer according to that. – Vishwas Bhatnagar Nov 24 '17 at 6:44
  • @VishwasBhatnagar I have added my answer after checking OP's comment on your answer, OP need to check customer's verification before visiting any page, the code will do the same – Piyush Nov 24 '17 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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