Magento: Redirect User to 404 Page

Magento: Redirect User to 404 Page

July 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Redirect User to 404 Page

Handling Redirection to a Custom 404 Page in Magento

In some cases, you might want to redirect your users to a custom 404 page when specific conditions are met in your Magento storefront. However, setting the HTTP status code correctly is crucial for both user experience and SEO purposes.

The Problem: Setting Incorrect Status Code

You’ve already implemented an event observer using the following code:

Mage::app()->getFrontController()->getResponse()->setHeader('HTTP/1.1', '404 Not Found', true);
Mage::app()->getFrontController()->getResponse()->setHeader('Status', '404 File not found', true);

$pageId = Mage::getStoreConfig('web/default/cms_no_route');
$url = rtrim(Mage::getUrl($pageId), '/');

Mage::app()->getFrontController()->getResponse()->setRedirect($url);

This code sets the headers for a 404 error and redirects to a specified URL. The issue here is that while you’ve set the HTTP status header, the actual response sent to the client uses a 302 (Found) status instead of 404.

The Solution: Correctly Setting the Status Code

To correctly implement this behavior in Magento, you’ll need to use the setHttpResponseCode method provided by Magento’s response object:

$response = Mage::app()->getResponse();
$response->setHttpResponseCode(404);
$response->sendHeaders();

Mage::app()->getFrontController()->getRequest()->setPathInfo('/path/to/your/custom-404-page.html');

Mage::app()
    ->getLayout()
    ->createBlock('page/html')
    ->render();

Here’s what this code does:

  1. It retrieves the current response object.
  2. It sets the HTTP response code to 404 using setHttpResponseCode.
  3. The sendHeaders method sends the headers to the client, including the set status code.

In your case, you can replace ‘/path/to/your/custom-404-page.html’ with the path to your custom 404 page template.

Why This Approach Works

The key difference lies in how Magento handles redirection. When using setRedirect, Magento internally uses a 302 status code for the redirect, even though you’ve set the headers correctly. By using the setHttpResponseCode method and manually rendering the layout, we bypass this built-in behavior and ensure that the actual response sent to the client matches the intended status code.

Conclusion

To effectively handle redirection to a custom 404 page in Magento while maintaining the correct HTTP status code:

  • Use the setHttpResponseCode method to set the desired status code (in this case, 404).
  • Send the headers using sendHeaders.
  • Manually set the path info and render the layout.

This approach ensures that your custom redirection maintains the intended meaning of a “Not Found” error, which is crucial for both user navigation and SEO purposes. Always remember to test your implementation thoroughly to ensure it works as expected across different browsers and scenarios.

For more detailed information on working with Magento’s response object and event observers, you can consult the official Magento documentation or visit our dedicated resources at https://laravelcompany.com/magento-development-resources.

Extensions Themes Configuration SEO

Our network