How to fix 301/302 redirect in Magento 2 programmatically

If you have an unexpected 301 or 302 redirects in Magento 2, you can easily find it by editing the following files:

/vendor/magento/framework/HTTP/PhpEnvironment/Response.php
/var/www/html/m2_35ee/vendor/magento/framework/Controller/Result/Redirect.ph

Open the response.php file and add the following lines at the beginning of the setRedirect function:

var_dump($url); \Magento\Framework\Debug::backtrace(false, true, false); exit();

Like:

public function setRedirect($url, $code = 302)
{
    var_dump($url); \Magento\Framework\Debug::backtrace(false, true, false); exit();
    $this->setHeader('Location', $url, true)
        ->setHttpResponseCode($code);

    return $this;
}

Open the Redirect.php file and add the following code:

var_dump($this->url); \Magento\Framework\Debug::backtrace(false, true, false); exit();

after each line containing,

$this->url =

Like:

public function setRefererUrl()
{
    $this->url = $this->redirect->getRefererUrl();
    var_dump($this->url); \Magento\Framework\Debug::backtrace(false, true, false); exit(); 
    return $this;
}

public function setRefererOrBaseUrl()
{
    $this->url = $this->redirect->getRedirectUrl();
    var_dump($this->url); \Magento\Framework\Debug::backtrace(false, true, false); exit(); 
    return $this;
}

public function setUrl($url)
{
    $this->url = $url;
    var_dump($this->url); \Magento\Framework\Debug::backtrace(false, true, false); exit(); 
    return $this;
}

public function setPath($path, array $params = [])
{
    $this->url = $this->urlBuilder->getUrl($path, $this->redirect->updatePathParams($params));
    var_dump($this->url); \Magento\Framework\Debug::backtrace(false, true, false); exit(); 
    return $this;
}

 

If you are looking for Magento Agency, visit Magento Developement Agency.

Related Products