How can I redirect 404 pages to Home Page in Magento Community?

How can I redirect 404 pages to Home Page in Magento Community?

July 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I redirect 404 pages to Home Page in Magento Community?

Redirecting 404 Pages to Home Page in Magento Community

Hello Lane,

Thank you for your question about redirecting 404 pages to the home page in Magento Community Edition. In this post, I’ll guide you through the necessary steps using the default theme and provide a clean solution without any extensions.

Understanding the Default 404 Behavior

By default, when a customer navigates to an invalid URL on your Magento store, they will see a “404 Not Found” error page. This is handled by the 404.php template file located in the app/design/frontend/{your_vendor}/{your_package}/templates directory.

Redirecting All 404 Errors to Home Page

To redirect all 404 errors to the home page, we need to modify the core Magento code. However, it’s essential to follow best practices and avoid direct modifications that may be overwritten during updates.

Using an Observer

The recommended approach is to use an observer to catch the controller_action_predispatch event before the 404 error is rendered. This allows us to redirect the user without touching the core template file directly.

Here’s how to implement this:

  1. Create a new module in your local code pool (if it doesn’t exist) using the Magento CLI: bin/magento setup:install:component <ModuleName>.

  2. In your newly created module, navigate to the etc directory and open the event.xml file.

  3. Add the following observer definition:

<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="redirect_to_homepage_if_404" instance="<Vendor>\<ModuleName>\Observer\Redirect404ToHomePage" />
    </event>
</config>

Replace <Vendor> and <ModuleName> with your actual vendor and module names.

  1. Create the observer class in app/code/<Vendor>/<ModuleName>/Observer:
<?php

namespace <Vendor>\<ModuleName>\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Controller\ResultFactory;

class Redirect404ToHomePage implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if ($observer->getAction()->getRequest()->getPathInfo() === 'no-route') {
            $resultRedirect = $observer->getControllerAction()->getResponse()
                ->representAs(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setHttpResponseCode(302);
            $resultRedirect->setUrl($observer->getControllerAction()->getUrl(''));
            return $resultRedirect;
        }
    }
}
  1. Clear your Magento cache: bin/magento cache:clean.

Conclusion

With this approach, whenever a 404 error occurs on your store (due to a non-existent URL or invalid route), the user will be automatically redirected to the home page without seeing the default “404 Not Found” error message.

Remember that using an observer allows you to maintain a clean codebase and easily manage redirection logic within your custom modules. If you need more advanced 404 handling, such as redirecting specific non-existent URLs, consider exploring Magento’s URL rewrite functionality or using extensions like Laravel Company’s SEO Suite Ultimate.

If you have any other questions about Magento development or need further assistance with your store setup, feel free to reach out. Happy customizing!

Best, [Your Name]

Cache Security Extensions Themes

Our network