Overriding magento cms controller for noRoute action

Overriding magento cms controller for noRoute action

September 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Overriding magento cms controller for noRoute action

Overriding Magento CMS Controller for NoRoute Action

You’re not alone if you’ve found it challenging to override the noRoute action of the Magento CMS controller. The documentation around this topic is scarce, and many tutorials are outdated or only partially correct. In this post, I’ll guide you through the process step-by-step using a compatible version of Magento (1.5 in your case).

Understanding the NoRoute Action

Before we dive into the code, let’s understand what the noRoute action does. This action is called when Magento can’t find a matching route for a given URL. By default, it renders a 404 page using the cms_index_no_route layout.

Overriding the Controller

To override this behavior, you need to create your own controller that replaces the built-in one. Here’s how:

  1. Create a new folder under app/code/local/YourCompany/YourModule/controllers/Cms/. Make sure your module is enabled in app/etc/modules/YourCompany_YourModule.xml.

  2. Inside this folder, create a file named IndexController.php and open it in your favorite IDE.

<?php

class YourCompany_YourModule_Cms_IndexController extends Mage_Core_Controller_Front_Action {

    public function noRouteAction() {
        // Your custom logic goes here
        $this->loadLayout();
        $this->renderLayout();
    }
}
  1. Save the file and clear your cache using php shell/compile.php in your Magento root directory.

Implementing Custom Logic

Now let’s implement the functionality you requested - sending an email when a 404 occurs.

Add the following code inside the noRouteAction() method:

public function noRouteAction() {
    // Prepare email data
    $emailTo = 'your.email@example.com';
    $emailSubject = '404 Page Not Found on Magento Site';
    $errors = Mage::getStoreConfig('system/log/Exceptions') ? true : false;
    if ($errors) {
        $errorString = Mage::log()->getLogMessages();
        $emailBody = "Page not found: " . $this->getRequest()->getRouteName() . '/' .
            $this->getRequest()->getControllerName() . '/' . $this->getRequest()->getActionName() . PHP_EOL .
            'Error log:' . PHP_EOL . trim($errorString);
    } else {
        $emailBody = "Page not found: " . $this->getRequest()->getRouteName() . '/' .
            $this->getRequest()->getControllerName() . '/' . $this->getRequest()->getActionName();
    }

    // Send the email
    try {
        Mage::getModel('core/email_template')->sendTransactional(
            'Your_Module_Name/404_email', // template ID
            'sales@yourcompany.com',
            $emailTo,
            null, // receiver name (null for default)
            array('name' => 'Customer Name'),
            $emailSubject,
            $emailBody
        );
    } catch (Exception $e) {
        Mage::log($e->getMessage());
    }

    $this->loadLayout();
    $this->renderLayout();
}

Make sure to replace Your_Module_Name with the actual name of your module and sales@yourcompany.com with your email address.

Conclusion

Overriding the noRoute action is a bit tricky due to Magento’s architecture. However, by following these steps exactly, you should be able to successfully implement this functionality in your Magento 1.5 installation.

Remember that overriding core controllers should always be approached carefully, as any changes can potentially break other parts of the system when you upgrade or update Magento.

For more advanced customizations and best practices in Magento development, I recommend checking out our comprehensive guide at https://laravelcompany.com/magento-development-best-practices. It covers everything from performance optimization to security hardening tips.

Cache Extensions Themes

Our network