38 votes

Redirect controller in magento 2

You can directly use below code in Action controller $this->_redirect('customer/account/');
Mohammad Mujassam's user avatar
32 votes

Redirect controller in magento 2

Use below code: protected $resultFactory; public function __construct( \Magento\Framework\Controller\ResultFactory $resultFactory, \Magento\Framework\App\Action\Context $context ) { $this-&...
Prashant Valanda's user avatar
31 votes
Accepted

Redirect all http:// request to https://

Changing the unsecure base url to https will change all links and redirect non-https requests to https://example.com/ (the home page), because that's what Magento does when the base URL validation ...
Fabian Schmengler's user avatar
25 votes
Accepted

Magento2: redirection from Observer

Yup, i have find a solution by myself by doing a research If you want to do that then on __construct() function of your class observer,you must inject two classes. First, \Magento\Framework\App\...
Amit Bera's user avatar
  • 77.3k
22 votes

Redirect controller in magento 2

public function execute() { $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setPath('routename/controllerpath/controllername'); return $resultRedirect; } No need ...
Hemant Kumar's user avatar
22 votes
Accepted

Magento 2 - Redirect user to specific page after register

I think the problem is that your redirect is occuring before the default redirect and hence has no effect. Here's a module that does the job: Module directory structure: | registration.php | +--...
Aaron Allen's user avatar
  • 9,009
20 votes
Accepted

Magento2 redirecting to Shopping cart

Like Magento1.x, there is an option in Magento2 admin panel to redirect to shopping cart page after adding product to cart. Change the option as, Stores->Configuration->Sales->Checkout->Shopping ...
saravanavelu's user avatar
  • 3,941
18 votes

Magento 2 Custom Admin Action Redirected to Dashboard

In routes.xml configuration file (companyName/customModule/etc/adminhtml/routes.xml). For me the solution was to have the route id and frontName have the same value. In your case this would be: <?...
Sankar_k's user avatar
  • 614
15 votes
Accepted

Magento 2: How to redirect customer to login page

If you want to do it directly from .phtml file use following code: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->get('\Magento\Customer\...
Bartłomiej Szubert's user avatar
12 votes

Magento2: redirection from Observer

injecting the \Magento\Framework\App\ActionFlag $actionFlag and $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);is the way of forcing Magento to stop ...
Zeeshan Khuwaja's user avatar
11 votes

Magento 2: How to redirect customer to login page

@Krupali, if you're adamant about your code being implemented in a template, then @Bartlomiej Szubert's example is the better choice. Generally, it's best practice to hide away those implementation ...
ryanF's user avatar
  • 2,269
10 votes

Redirect all http:// request to https://

For completeness we also do a very similar thing to what fschmengler has suggested with some additions; RewriteCond %{HTTP:X-Forwarded-Proto} !=https RewriteCond %{HTTPS} !=on RewriteCond %{...
Smartie's user avatar
  • 3,240
10 votes

magento2 - how to redirect to 404 page

There are some methods discussed on different forums but the one worked for me is as follow. <?php namespace Company\ModuleName\Controller\Index; use Magento\Framework\App\Action\Context; use \...
Muhammad Saeed Khan's user avatar
10 votes
Accepted

magento 2 redirect url with parameters from controller

You can pass param like this : $params = array('myname' => 'Nuno Sousa'); $resultRedirect->setPath('customer/index/new', ['params' => $params]); You can retrive using this : $this->...
Rohan Hapani's user avatar
  • 17.3k
9 votes

Redirect all http:// request to https://

Set your base url for secure and unsecure in the backend. Set yes on use secure url in frontend in the backend. Then edit your app/etc/local.xml and include this <?xml version="1.0"?> <...
Michael's user avatar
  • 540
9 votes

What type of redirect is this?

If you need to know detail about the following code then read the following: $resultRedirect->setPath('*/*/'); First * means current route and second * means current controller. Let me explain it ...
Sukumar Gorai's user avatar
8 votes

Magento2: redirection from Observer

Above answer is not working for me I have updated my observer code public function execute(\Magento\Framework\Event\Observer $observer) { $params = []; // If you want to ...
Himanshu's user avatar
  • 1,761
8 votes

Contact form after submission not redirecting to cms page Magento 2

Contact Post Controller needs to be overridden here, because by default it redirects to contact/index $this->_redirect('contact/index'); needs to be changed to $this->_redirect('cms-page-url-...
Anshu Mishra's user avatar
  • 8,950
7 votes

Redirect controller in magento 2

Try this code : namespace Company\Module\Controller\Index; use Magento\Framework\Controller\ResultFactory; class Actionname name extends \Magento\Framework\App\Action\Action { ...
DEEP JOSHI's user avatar
7 votes
Accepted

Magento 2: How to Redirect from Observer to CMS Page

protected $responseFactory; protected $url; public function __construct( ... \Magento\Framework\App\ResponseFactory $responseFactory, \Magento\Framework\UrlInterface $url ... ) { $...
Prince Patel's user avatar
  • 22.7k
7 votes

magento 2 redirect url with parameters from controller

I am using Magento 2.3.4 $params = array('myname' => 'Nuno Sousa'); Somehow this did not work for me. $resultRedirect->setPath('[modulename]/[controllername]/[actionname]', ['params' => $...
Klaus Mikaelson's user avatar
6 votes

Magento 2 Custom Admin Action Redirected to Dashboard

Maybe it's better to use that property: /** * Array of actions which can be processed without secret key validation * * @var array */ protected $_publicActions = ['action_name'];
Magento's God's user avatar
6 votes

How can I redirect my Home Page in Magento? (301 permanent redirect)

You should set ../ as target path, see the screenshot below: Now request to www.domain.com/network-nvr201-08lp will redirect to www.domain.com.
Konstantin Gerasimov's user avatar
6 votes
Accepted

Magento2: check if url is an admin url

From index.php, before anything initializes? Realistically, you can't. You could add an explicit check for your admin path in the URL, and skip it if so, but that entire thing makes for a very dirty ...
Ryan Hoerr's user avatar
  • 12.2k
6 votes
Accepted

category url redirect to custom controller - magento 2

In order to full fill your requirement, can to use Event/Observer Create a custom module and using catalog_controller_category_init_after event for redirect to your custom url see Magento2: ...
Amit Bera's user avatar
  • 77.3k
6 votes

Magento CMS Page keep redirect to homepage

Open your admin panel of magento. Goto System>Configuration>>Web> URL Option and change Auto-redirect to Base URL to No.
Nikul's user avatar
  • 1,306
6 votes

How to block cpanel link of my magento site

First thing you need to understand is that: for a webserver, both the owner and normal site visitors are the same. There is no default mechanism to identify a "Site owner". To achieve your goals, you ...
Nidheesh's user avatar
  • 1,048
6 votes
Accepted

Redirecting HTTP to HTTPS?

I fixed it by adding RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://mydomain.co.uk/$1 [L,R=301] To my .htaccess and setting Auto-Redirect to base url to No Hope this helps someone
AspiringProgrammer's user avatar
6 votes
Accepted

Magento 2 : Redirect From Homepage to Login if not Logged

Try following way: Iuk/Homeredirect/etc/frontend/events.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:...
Sohel Rana's user avatar
  • 35.7k
6 votes
Accepted

How to redirect to customer account login page using Ajax in magento 2?

We can set the redirect url in our ajax response. For example: <?php namespace Vendor\Customer\Controller\Ajax; use Magento\Framework\App\Action\Context; use Magento\Customer\Model\Session; use ...
Khoa TruongDinh's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible