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 .. :)
user avatar
  • 974
33 votes
Accepted

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

This is the code that should be used in Drupal 8. See change Record for more info. use Symfony\Component\HttpFoundation\RedirectResponse; function my_goto($path) { $response = new ...
user avatar
30 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 ...
user avatar
  • 89.4k
25 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 ...
user avatar
  • 642
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 ...
user avatar
  • 13.5k
23 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 ...
user avatar
  • 1,065
18 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: &...
user avatar
  • 89.4k
18 votes

Redirect after user login

Redirecting users after they login 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 ...
user avatar
  • 94.6k
17 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('&...
user avatar
  • 5,933
17 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 ...
user avatar
  • 89.4k
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, ...
user avatar
  • 81.6k
14 votes

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

The easiest way is to browse to /admin/config/system/site-information and fill in the field that reads Default 403 (access denied) page with user/login
user avatar
  • 547
13 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. /** * ...
user avatar
  • 131
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: ...
user avatar
  • 309
13 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>');
user avatar
  • 89.4k
12 votes
Accepted

How to use the Rules module to implement a custom redirect for an outdated URL?

Why using .htaccess might not be an option There are situations where setting up a redirect in your .htaccess file may not be an option, such as: you are not allowed (= do not "have") edit access to ...
user avatar
12 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, ...
user avatar
  • 948
11 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 ...
user avatar
  • 21k
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 ...
user avatar
10 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 ...
user avatar
  • 201
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) { ...
user avatar
  • 738
9 votes

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

Just looking at the title of this question (= How to redirect user to login form from 403?), there are 2 options to do so, without any custom code involved, as detailed below. Option 1: Use the ...
user avatar
9 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()...
user avatar
  • 81.6k
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-...
user avatar
8 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. ...
user avatar
  • 415
8 votes
Accepted

Remove or Redirect Users who go to www.domain.com to domain.com

If you're using Apache with your setup, this is built into the .htaccess file that comes with the distribution of Drupal. Look for the following code and follow the instructions in the comments: # ...
user avatar
  • 26.4k
8 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 ...
user avatar
  • 3,176
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.
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 ...
user avatar
8 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);
user avatar
  • 1,329

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