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 .. :)
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 ...
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 ...
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 ...
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 ...
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:
&...
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('&...
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 ...
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 ...
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, ...
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>');
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.
/**
* ...
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:
...
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, ...
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 ...
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 ...
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 ...
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()...
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);
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) {
//...
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 ...
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.
...
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) {
...
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 ...
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-...
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,...
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.
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 ...
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')
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
redirection × 8487 × 256
8 × 158
users × 123
forms × 90
uri × 73
nodes × 50
rules × 48
views × 43
path-aliases × 43
.htaccess × 41
6 × 35
routes × 22
i18n-l10n × 22
webforms × 20
hooks × 19
9 × 14
multi-sites × 14
commerce × 13
taxonomy-terms × 13
caching × 13
files × 12
navigation × 11
ajax × 10
ssl × 10