{"id":956,"date":"2020-04-19T09:02:43","date_gmt":"2020-04-19T09:02:43","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/04\/19\/redirect-to-previous-page-in-magento-2\/"},"modified":"2025-05-22T13:29:22","modified_gmt":"2025-05-22T07:59:22","slug":"redirect-to-previous-page-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/redirect-to-previous-page-in-magento-2\/","title":{"rendered":"How To Redirect To Previous Page In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 CMS allows customization in order to improve the user experience.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below post is a similar example that allows to&nbsp;redirect to previous page in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now,&nbsp;a user filled a form on your store and clicked the submit button. The user\u2019s data is saved in a file and you would want to display a message of a successful form submission or an error as per my earlier post to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/display-error-success-warning-message-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">display error, success, &amp; warning Message In Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For that you need to redirect the user to the previous page URL and hence use this solution:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to redirect to previous page 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=\"\">namespace Company\\Module\\Controller\\Index;\nuse Magento\\Framework\\Controller\\ResultFactory;class Actionname name extends \\Magento\\Framework\\App\\Action\\Action\n{     \n    public function execute()\n    {\n        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);        \n\/\/ Your code        \n$resultRedirect->setUrl($this->_redirect->getRefererUrl());\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 the 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>Magento 2 CMS allows customization in order to improve the user experience. The below post is a similar example that allows to&nbsp;redirect to previous page&#8230;<\/p>\n","protected":false},"author":5,"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-956","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/956","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=956"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/956\/revisions"}],"predecessor-version":[{"id":14958,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/956\/revisions\/14958"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}