Skip to main content
The 2024 Developer Survey results are live! See the results
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
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
  • 624
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,279
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.5k
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,776
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,990
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.8k
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

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

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
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
6 votes

ERR_TOO_MANY_REDIRECTS error when setting up HTTPS

Just to close this out, a good friend of mine suggested I try this, and I am very pleased to say it worked. Perhaps it will help someone else who faces the same issue: In .htaccess file, add these ...
Nick Hoare's user avatar
6 votes
Accepted

Website start redirecting to another url

I found the injected code in the core_config_data table, under design/head/includes. Removed it and now the site is back to normal. UPDATE: As everyone else mentioned, it happened again this morning. ...
Boris K.'s user avatar
6 votes
Accepted

Change logout redirect link to external page

Override the logout phtml template and update the redirect url. vendor/magento/module-customer/view/frontend/templates/logout.phtml:14 Change the $block->getUrl() to the url where you want to be ...
Mike's user avatar
  • 1,148
6 votes

Admin Categories Page Infinite 302 Redirection after migrating the data from 1.9.3.6

Check your root_category_id in table store_group That solved the problem for me!
Flo69's user avatar
  • 161
6 votes

How to remove trailing slash from Magento 2 url

Magento admin you can use store direct_url for remove trailing slash. example: <a href="{{store direct_url='contact-us'}}">Contact</a>
Savan Patel's user avatar
  • 2,366
6 votes

Magento 2 with Varnish sometimes does not redirect HTTP to HTTPS

The issue was the Pound was only listening to 443 (https) and all HTTP was going straight to Varnish. Magento was supposed to be redirecting but sometimes Varnish would just send the HTTP version. ...
YWSW's user avatar
  • 336
5 votes

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

Make sure System > Configuration > Web > url_options > "Auto-redirect to Base URL = No". It was set to yes, and causing the problem. Put below code in .htaccess file. RewriteCond %{HTTP:...
Vikas Verma's user avatar
5 votes
Accepted

Redirect in Magento 2 from PHTML file

Add this code in your phtml file.... $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $redirect = $objectManager->get('\Magento\Framework\App\Response\Http'); $redirect->...
Rohan Hapani's user avatar
  • 17.5k
5 votes
Accepted

How to redirect from model

No worries! We also can redirect from Model. Just try the following code in your model: Add below code as parameter in __construct method of your Model \Magento\Framework\UrlInterface $url, \...
Irfan Momin's user avatar
5 votes

What type of redirect is this?

It is used to redirect current route, current controller's index action. Let say if you are currently browsing checkout/index/customaction if you are using this code in custom action controller, ...
Himmat Paliwal's user avatar
5 votes

Magento 2 redirect to external URL from controller

This code is worked for me protected $resultRedirectFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Controller\Result\Redirect $...
Hitesh Balpande's user avatar

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