36 votes
Accepted

Redirect after form submission

In your "submitForm" method write below code $form_state->setRedirect('machine_name'); return; where machine_name is the machine name mentioned in the routing file. I hope this helps .. :)
parth.k's user avatar
  • 1,004
32 votes
Accepted

How to create a redirection programmatically?

If you want to store redirects in database use the module Redirect. This module provides a user interface to add redirects. The redirects are stored in a content entity, which you can also create ...
4uk4's user avatar
  • 98.1k
31 votes

What function/method can I use to redirect users to a different page?

First (but read on for the correct, better aproach) it's important to note that Drupal 9.2 broke the simple way of doing it: $response = new RedirectResponse($path); $response->send(); You have to ...
Gábor's user avatar
  • 801
24 votes

Redirect after user login

You need to alter the login form and add a submit handler which will take care of the redirection. You cannot use $form_state->setRedirectUrl() directly in the form alter, since it will be ...
pfrenssen's user avatar
  • 1,137
24 votes
Accepted

Redirect Anonymous user to login page

You can test a user's status very early with event subscriber in a custom module that subscribes to KernelEvents::REQUEST. First, you register the event subscriber in mymodule.services.yml in your ...
oknate's user avatar
  • 14.1k
20 votes

How to redirect an anonymous user to the login form after a 403 error?

I've seen that this question was never answered how to do this programmatically. The code actually does work, when placed in an Exception Subscriber: /src/EventSubscriber/RedirectOn403Subscriber.php: &...
4uk4's user avatar
  • 98.1k
19 votes
Accepted

Redirect to the front page with a different language

You can try this code <?php use Drupal\Core\Url; use Symfony\Component\HttpFoundation\RedirectResponse; $language = Drupal::languageManager()->getLanguage('vi'); $url = Url::fromRoute('&...
MrD's user avatar
  • 6,148
19 votes
Accepted

How can I turn off caching redirect?

A response built from the RedirectReponse class is not cached. So you should not need to set any cache metadata. The problem here is probably the page cache for anonymous requests, which caches all ...
4uk4's user avatar
  • 98.1k
18 votes

Redirect after user login

Redirecting users after they log in on a Drupal 8 site is not different from how it was done on Drupal 7, except the code needs to be adapted for Drupal 8. In particular: hook_user_login() is not ...
apaderno's user avatar
  • 97k
17 votes
Accepted

Force redirect in submit handler even though destination parameter set

The only correct way to do this is to alter the Request object and drop the destination. In your submit callback: \Drupal::request()->query->remove('destination'); Depending on the context, ...
Berdir's user avatar
  • 82.9k
16 votes
Accepted

How to redirect to frontpage from a controller

Use the route <front> and when you are in a controller use the injected method redirect(): return $this->redirect('<front>');
4uk4's user avatar
  • 98.1k
15 votes

Redirect after user login

Bit late to the party but as per https://www.drupal.org/node/2068293#comment-11712455 you can set the destination in hook_user_login() to redirect at the end of the login processing. i.e. /** * ...
Sut3kh's user avatar
  • 151
13 votes
Accepted

How can I redirect node page for a content type to custom URL?

Here is how I managed to solve this issue, based on the example in drupal.org/node/2013014: I registered Event Subscriber in my_redirect_module.services.yml file: services: ...
Aghiad's user avatar
  • 309
13 votes
Accepted

How to redirect to external URL upon form submission

You'll want to use $form_state->setResponse() instead to utilize the TrustedRedirectResponse you're creating. Something like this should work: public function submitForm(array &$form, ...
hampercm's user avatar
  • 958
12 votes

Redirect after user login

there are couple of solutions to do this: if you want to do it in your custom module: Solution 1: use a custom form submit hander /** * Implements hook_form_alter(). */ function ...
Yuseferi's user avatar
  • 21.8k
11 votes

redirect whole site to HTTPS

RewriteCond %{HTTPS} !=on # This checks to make sure the connection is not already HTTPS RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] # This rule will redirect users from their original ...
rjkunde's user avatar
  • 211
10 votes

How to perform a redirect to custom page after node save or delete?

Basically the code will work for submit action alone, but drupal node save form have many actions, I think you would have logged in as admin and trying it, then follow the code below. if you want ...
Karthikeyan Manivasagam's user avatar
10 votes
Accepted

TrustedResponseRedirect failing - how to prevent cache metadata?

You mentioned the URL generator, which is one of the two ways this happens. In short, you need to do something like my patch in this issue: https://www.drupal.org/node/2630808 Pass TRUE to toString()...
Berdir's user avatar
  • 82.9k
10 votes

Redirect after form submission

If you want to set at buildform you need to use some routing path like. use Drupal\Core\Url; $url = Url::fromRoute('route.path'); $form_state->setRedirectUrl($url);
vgoradiya's user avatar
  • 1,359
10 votes
Accepted

How should I perform a redirect in hook_form_alter()?

use Drupal\Core\Form\FormStateInterface; /** * Implements hook_form_FORM_ID_alter(). */ function [MODULENAME]_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) { //...
harsh_behl_0007's user avatar
10 votes
Accepted

Disabling Page Cache for Redirect Response

This doesn't exactly answer your question, but I feel it's relevant information. I did something similar, but I also didn't like using the kill switch. In another iteration I used a ...
sonfd's user avatar
  • 7,811
9 votes

What function/method can I use to redirect users to a different page?

This can be achieved by leveraging built-in symphonies EventDispatcher Component. All you have to do is create a custom module. Add your services.yml file and provide appropriate service config. ...
Jay Chand's user avatar
  • 425
9 votes

What function/method can I use to redirect users to a different page?

To build on Anu Mathew's response; To add a status code, its just the second param in the RedirectResponse class; use Symfony\Component\HttpFoundation\RedirectResponse; function my_goto($path) { ...
Christian's user avatar
  • 752
9 votes

How to redirect to a edit node page with RedirectResponse?

Note that \Drupal::url() is deprecated. You should use the static methods of the Url class to create URLs from routes. To find the route names, use the Drupal console drupal router:debug. From that I ...
cilefen's user avatar
  • 5,557
9 votes

Redirect Anonymous user to login page

First, create a service for your event subscriber in module-name.services.yml Code - services: [MODULE-NAME]_event_subscriber: class: Drupal\MODULE-NAME\EventSubscriber\[Event-Subscriber-...
Ashish Deynap's user avatar
8 votes

What function/method can I use to redirect users to a different page?

Never use $response->send() under any circumstances. This results in unpredictable behavior and might fail in different environments, when moving from dev to prod, for example, or in future updates,...
4uk4's user avatar
  • 98.1k
8 votes

Redirect after user login

You can simply do it by using Rules React on: After user has logged in Add action: Redirect >> then use a parameter or type your url.
Shrikant Nakade's user avatar
8 votes

Redirect after user login

In D8 and later, you can use user default page module for this purpose. This module allows you to customize the destination that a user is redirected to after logging in or logged out. You can ...
Pallavi Chaudhury's user avatar
8 votes
Accepted

Redirect using a path

You can also use setRedirectUrl(), which needs a URL object. There are many ways to create one, for example for an arbitrary path: Url::fromUri('base:some/path')
Berdir's user avatar
  • 82.9k
8 votes
Accepted

Redirect a form to an external URL

The method that you missed is $form_state->setResponse(). In combination with a TrustedRedirectResponse object, which exists for security reasons (to prevent users from accidentally being ...
Berdir's user avatar
  • 82.9k

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