Magento Product Page redirection

Magento Product Page redirection

July 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Product Page redirection

Redirecting Magento Product View Pages: A Comprehensive Guide

As a senior developer specializing in e-commerce platforms like Magento, I often encounter questions about customizing product pages. One common request is to redirect specific product view pages to different URLs. Let’s dive into the ideal way to accomplish this using Magento 2.

The Controller That Needs Overriding

In Magento 2, you’ll need to override the Magento\Catalog\Controller\Product\View controller in order to handle redirection for individual products. This is the primary controller responsible for rendering product detail pages.

Here’s a quick example of how you’d go about creating an override:

<?php

namespace YourCompany\YourModule\Controller\Product;

use Magento\Framework\App\ActionInterface;
use Magento\Framework\Controller\ResultFactory;

class View extends \Magento\Catalog\Controller\Product\View
{
    public function execute()
    {
        $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);

        // Add your redirection logic here

        return $resultPage;
    }
}

In this example, we’re simply creating a new class that extends the original Magento\Catalog\Controller\Product\View controller and defining our own execution method. This allows us to add custom logic while still leveraging Magento’s built-in functionality.

Implementing the Redirection Logic

Now let’s address the redirection itself. You asked for a “more Magento-like way” of handling this, which involves using Magento’s URL management system rather than relying on PHP’s header() function.

First, you’ll need to inject the Magento\Framework\UrlInterface into your controller:

private $urlBuilder;

public function __construct(
    \YourCompany\YourModule\Controller\Product\View $context,
    \Magento\Framework\UrlInterface $resultPageFactory,
    \Magento\Framework\UrlInterface $urlBuilder
) {
    parent::__construct($context, $resultPageFactory);

    $this->urlBuilder = $urlBuilder;
}

Next, you can use the getUrl() method of $urlBuilder to generate your redirection URL:

if ($this->getProductId() == 10) {
    $redirectUrl = $this->urlBuilder->getUrl('yourmodule/yourcontroller/index');
    return $this->resultRedirectFactory->create()->setPath($redirectUrl);
}

In this code, we’re using the getProductId() method to retrieve the current product’s ID. If it matches our desired product (ID 10 in this case), we generate a URL for ‘yourmodule/yourcontroller/index’ and redirect to that path.

Conclusion: A Cleaner Approach

While your initial code snippet works, using Magento’s built-in URL management system provides several benefits:

  • It adheres to the MVC pattern, keeping business logic separate from presentation.
  • It ensures compatibility with future updates and improvements in Magento’s core.
  • It allows you to leverage features like SEO-friendly URLs and caching.

Remember, when working with Magento 2, it’s always best to override controllers rather than altering core files directly. This ensures your changes are preserved during upgrades and can be easily maintained.

For more advanced redirection scenarios or if you’re looking to optimize the performance of your store, I recommend checking out our comprehensive guide on Magento performance optimization. It provides a deep dive into best practices for keeping your e-commerce site running smoothly.

Magento 2 Catalog Extensions Orders

Our network