0

How to show 404 for the unlogged user instead of page? For now, I have: when the user is not logged the page link does not show in the main menu. But when I type the URL (page.com/my_page) I get redirect to my_page.

Any help would be appreciated. Thanks in advance.

7
  • You mean if user not logged in you want to redirect them to 404 page ?
    – Ravi Soni
    Jul 1, 2019 at 12:37
  • @RaviSoni, thank you for your comment. Unfortunately I'm new to magento. Correct me if i'm wrong. I have to create custom module for that? And in config.xml I have to define events?
    – SebastianT
    Jul 1, 2019 at 12:41
  • Yes, you have to define it in config.xml. But what your exact requirement? If user not logged in, you want them to redirect on 404 page. Right? In short, you don't allow the guest user on your website. Right?
    – Ravi Soni
    Jul 1, 2019 at 12:45
  • Yes, that's right. I have page for example "my_page" and if i go to page.com/my_page i want to redirect to 404 if i'm not logged in.
    – SebastianT
    Jul 1, 2019 at 12:48
  • It's a cms page?
    – Ravi Soni
    Jul 1, 2019 at 12:50

2 Answers 2

1

Try this one:

/app/etc/modules/Custom_Module.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Module>
            <codePool>local</codePool>
            <active>true</active>
        </Custom_Module>
    </modules>
</config>

/app/code/local/Custom/Module/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Module>
            <version>0.0.1</version>
        </Custom_Module>
    </modules>
    <global>
        <models>
            <custommodule>
                <class>Custom_Module_Model</class>
            </custommodule>
        </models>
        <helpers>
            <custommodule>
                <class>Custom_Module_Helper</class>
            </custommodule>
        </helpers>
    </global>
    <frontend>
        <events>
            <controller_action_predispatch>
                <observers>
                    <Custom_Module_Model_Observer>
                        <type>singleton</type>
                        <class>custommodule/observer</class>
                        <method>noRoutUrl</method>
                    </Custom_Module_Model_Observer>
                </observers>
            </controller_action_predispatch>
        </events>
    </frontend>
</config>

/app/code/local/Custom/Module/Model/Observer.php

<?php 
class Custom_Module_Model_Observer extends Varien_Event_Observer {
    public function noRoutUrl($observer) {
        if ((strpos(Mage::helper('core/url')->getCurrentUrl(),'my_page') != false) && (! Mage::getSingleton('customer/session')->isLoggedIn()))  {
            Mage::app()->getResponse()->setRedirect('/no-route');
        }
    } 
}
?>

/app/code/local/Custom/Module/Helper/Data.php

<?php

class Custom_Module_Helper_Data extends Mage_Core_Helper_Abstract{

}

This the step you have to follow by creating all files

3
  • Thank You very much, it works perfect! Could you tell me, where can i find informations about "controller_action_predispatch" and others events. Where to find name of event which i want to use?
    – SebastianT
    Jul 1, 2019 at 13:24
  • Here is the link for all events: nicksays.co.uk/magento-events-cheat-sheet-1-9
    – Ravi Soni
    Jul 1, 2019 at 13:28
  • thank you a lot!
    – SebastianT
    Jul 2, 2019 at 5:45
0

You can try this below code:

<?php if (strpos(Mage::helper('core/url')->getCurrentUrl(),'my_page') != false && ! Mage::getSingleton('customer/session')->isLoggedIn())  {

            Mage::app()->getResponse()->setRedirect('/no-route');

        }
2
  • Hi, thanks for answer, could you explain please where to paste this code?
    – SebastianT
    Jul 1, 2019 at 12:59
  • @SebastianT, If You know how to use event observer in Magento then follow this link: magento.stackexchange.com/questions/181579/… and under the Observer.php add you r logic. which i have provided you. Jul 1, 2019 at 13:05

Your Answer

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

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