Forcing Magento 404 Pages to Return HTTP Status 301
As an experienced e-commerce developer specializing in Magento, I frequently encounter requests like yoursâto force all Magento 404 pages to return an HTTP status of 301 instead of the default 404.
The key lies in understanding how Magento handles error pages and then overriding this behavior using a combination of layout files and event observers. Let me guide you through this process with clear steps and practical examples.
The Default Magento Error Handling
By default, when Magento encounters a non-existent URL (a 404 page not found), it returns an HTTP status code of 404. This is the standard behavior for most web applications.
However, there are times when you might want to change thisâperhaps to redirect users to a more relevant or user-friendly page, or to implement SEO best practices by indicating that the content has permanently moved (status 301).
Overriding the Error Page Behavior
To achieve this in Magento, we can’t directly modify the layout.xml files. Instead, we need to create an event observer that listens for the controller_action_predispatch event and then manually sets the HTTP header before the page is rendered.
Here’s a step-by-step guide:
- Create an Event Observer
In your custom module (let’s call it “MyCompany_Seo”), create a new file at app/code/MyCompany/Seo/etc/frontend/events.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/event.xsd">
<event name="controller_action_predispatch">
<observer name="mycompany_redirect_404_to_301" instance="MyCompany\Seo\Observer\Redirect404To301" />
</event>
</config>
This file registers an observer for the controller_action_predispatch event.
- Create the Observer
Now, create a new class at app/code/MyCompany/Seo/Observer/Redirect404To301.php:
namespace MyCompany\Seo\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Response\Http as HttpResponse;
use Zend_Http_Response;
class Redirect404To301 implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var \Magento\Framework\Controller\ResultInterface $result */
$result = $observer->getControllerAction()->getResponse();
if ($result->isRedirect() || $result->getHttpResponseCode() != 404) {
return;
}
// Create a new response with status code 301
$response = new HttpResponse();
$response->setStatusCode(HttpResponse::HTTP_MOVED_PERMANENTLY);
// Get the original request URL and set it in the Location header
$request = $observer->getRequest();
$url = $request->getUri();
$headers = array('Location' => $url);
$response->setHeaders($headers);
// Send the response
$result->setHttpResponseCode(HttpResponse::HTTP_MOVED_PERMANENTLY);
}
}
This observer checks if the current action is a 404 error, and if so, creates a new HTTP response with status code 301. It then sets this as the new response for the controller.
Conclusion
Forcing Magento to return an HTTP status of 301 instead of 404 involves creating an event observer that intercepts the page rendering process and manually sets the appropriate headers.
This approach allows you to maintain control over your site’s error handling while keeping it in line with SEO best practices, especially for pages where content has permanently moved or changed URLs.
Remember, implementing this change should be done carefully, as incorrect usage of 301 redirects can impact your site’s crawlability and search engine rankings. Always ensure you have a clear understanding of where these redirects are needed before proceeding.
For more in-depth guides on Magento development best practices, feel free to explore our Magento Development section at LaravelCompany.com.
Happy coding!