{"id":1609,"date":"2021-02-20T03:30:47","date_gmt":"2021-02-20T03:30:47","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/create-referer-url-in-magento-2\/"},"modified":"2025-07-16T18:00:37","modified_gmt":"2025-07-16T12:30:37","slug":"create-referer-url-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-referer-url-in-magento-2\/","title":{"rendered":"How to Create Referer URL in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/support.google.com\/google-ads\/answer\/2382957\" target=\"_blank\" rel=\"noreferrer noopener\">Referer URL<\/a>&nbsp;is the address of the webpage where a person clicked a link that sent them to your page. Referer is the webpage that the visitor was on before landing on your page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When a non-logged in user tries to add the product to the wishlist, the default\u00a0Magento 2\u00a0generates one referer URL and redirects that user to the login page. For example, if you write customer\/account after your store URL without logging in, Magento 2 automatically redirects you to the login page using the referer URL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s the default behaviour of the Magento 2, but what if you want the same concept in other pages and functionalities on your store also?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can&nbsp;<strong><em>create referer URL in Magento 2&nbsp;<\/em><\/strong>to implement the below functionalities in your store.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Restrict user to access order page directly without login.<\/li>\n\n\n\n<li>Direct URL access restriction without logging in.<\/li>\n\n\n\n<li>Restrict payments using a specific method for non-logged-in users.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Follow the below solution and restrict the user to access your pages directly without login and get rid of unauthenticated users.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Create Referer URL in Magento 2<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Framework\\UrlInterface;\n\nclass Index extends Action\\Action\n{\n    protected $urlInterface;\n\n    public function __construct(Action\\Context $context, UrlInterface $urlInterface)\n    {\n        $this->urlInterface = $urlInterface;\n        return parent::__construct($context);\n    }\n\n    public function execute()\n    {\n        $url = $this->_redirect->getRefererUrl();\n        $login_url = $this->urlInterface\n            ->getUrl('customer\/account\/login',\n                array('referer' => base64_encode($url))\n            );\n        $resultRedirect = $this->resultRedirectFactory->create();\n        $resultRedirect->setUrl($login_url);\n        return $resultRedirect;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share this solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Referer URL&nbsp;is the address of the webpage where a person clicked a link that sent them to your page. Referer is the webpage that the&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1609","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1609"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1609\/revisions"}],"predecessor-version":[{"id":14484,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1609\/revisions\/14484"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}