How to get referer URL in Magento 2.

Magento 2, Get Referer url by calling function getRefererUrl() or getRedirectUrl() from RedirectInterface Object.

Magento core Interface, Magento\Framework\App\Response\RedirectInterface used for fetching Referer URL.

In PHP you can call simply by, $_SERVER[“HTTP_REFERER”], With Magento Best Practice, you need to call RedirectInterface in your __construct() function and get the 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();

This is the correct way, You can retrieve the referer URL in Magento.