The code does not show the referrer URL of the website. From which the user has come to the current page. Instead, it is displaying the website base URL any idea why it is happening? It is working fine on my website. when the user moves from product to contact page etc. I also tried this too.


but not working.

Here is my current code

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $redirect = $objectManager->create('\Magento\Framework\App\Response\RedirectInterface');
             echo $redirect->getRefererUrl();

Output: http://www.yourwebsite.com/

Expected Result: http://www.previouswebsite.com/

  • any solution to this?
    – sivakumar
    Oct 21, 2021 at 8:55

2 Answers 2


In PHP you can call simply by, $_SERVER[“HTTP_REFERER”], In Magento 2 Best Practice, you need to call RedirectInterface in your __construct() function and get referrer URL.

public function __construct(
    \Magento\Framework\App\Response\RedirectInterface $redirect
) {
    $this->redirect = $redirect;

Call below the line in your function,

echo $this->redirect->getRefererUrl();
echo $this->redirect->getRedirectUrl();

copied from RAKESH JESADIYA blog


I had the same question and found several answers that use \Magento\Framework\App\Response\RedirectInterface.

At least as of 2.3.4, using the getRefererUrl() and getRedirectUrl functions of this class does not reliably return the same value as $_SERVER['HTTP_REFERER']

I grepped the vendor/framework folder for "HTTP_REFERER" and found that \Magento\Framework\HTTP\Header includes the function:

 * Retrieve HTTP REFERER
 * @param boolean $clean clean non UTF-8 characters
 * @return string
public function getHttpReferer($clean = true)
    return $this->_getHttpCleanValue('HTTP_REFERER', $clean);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.