05.
The Art of Redirecting
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
7 Comments
Hello, my name is Stéphane and I am writing from Reunion Island. I just subscribed to your courses to refresh my knowledge in PHP. I am trying the redirect thing, but when I submit the form, I got the following message : "Warning: Cannot modify header information - headers already sent by...". And I guess it's because we have the "require 'layout/header.php'" instruction at the beginning of pets_new.php which already contains some html. So, how can I modify the code to make it redirect me to the homepage ?
Edit : Nevermind. I found the solution by moving the require instruction a few lines below just before the html code of the page.
Great courses !
Hey Stéphane,
Yeah, nice workaround! The another common way to solve this issue is to use output buffering for PHP. Enabling it in php.ini will allow PHP to buffer output instead of passing it to the web server instantly. PHP thus can aggregate HTTP headers even after your script already "echo" something to the browser.
Cheers!
Hi, I have an issue with redirects. Every now and then, my webhost's Nginx returns a "504 Bad Gateway" I have asked them for the logs, and they came back with this error message:
httpd[48541] [core:error] [pid 48541] [client 193.12.157.36:0] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary.<br />I only do 1 redirect in my code, like this:
return $this->redirectToRoute('operator_index');
But apparently that puts it through the roof of 10 (?). I also found some information (https://stackoverflow.com/questions/63728070/symfony5-too-many-redirect) that there may be a conflict between Symfony5 wanting to remove trailing slashes, and some webservers adding them back, (perhaps creating a redirect loop?)
My routing actually has two (probably unneccessary) trailing slashes
position_index GET ANY ANY /positionlog/positions/
operator_index GET ANY ANY /positionlog/operators/
But I don't know if that actually causes a 504 error?
I thought I probably wasn't the first one facing an issue with symfony redirects causing server problems.
I also just found out that faulty locale can cause an increased number of redirects. I had mine set to "se" which is incorrect, now changed to "sv" for swedish. I could also set it to "sv-SE"to include language AND country. However, not sure if that is responsible for a number of 504 errors =)
Howdy Matt,
I typically do not add a trailing slash when defining routes because Symfony will redirect the request if the slash is not provided with
GETrequests.e.g.
#[Route('/some/path/')]and the requesting URL is/some/path, Symfony will301redirect to/some/path/and vice versa.Hope this helps!
Side Note: Here is a link to the docs page that references this: https://symfony.com/doc/current/routing.html#redirecting-urls-with-trailing-slashes
Couldn't you just check if the record already exists? If the user is editing their profile info they might not want to get redirected, maybe they want to stay on the profile page and verify that their info was all correct and actually saved to boot??
Hey RU,
Even in this case you need to redirect users, but instead of a different page you redirect them to the same page. Without redirect, when you sent POST request, if you try to reload the page - browser will ask you a message like: "Are you sure you want to resend your POST request?". And that's not something you want. To avoid this, you need that redirect. But redirect could lead to any URL, even to the same URL users are currently on.
Cheers!
"Houston: no signs of life"
Start the conversation!