Symfony redirect to external URL
68,488
Solution 1
Answer to your question is in official Symfony book.
http://symfony.com/doc/current/book/controller.html#redirecting
public function indexAction()
{
return $this->redirect('http://stackoverflow.com');
// return $this->redirect('http://stackoverflow.com', 301); - for changing HTTP status code from 302 Found to 301 Moved Permanently
}
What is the "URL"
? Do you have really defined route for this pattern? If not, then not found error is absolutelly correct. If you want to redirect to external site, always use absolute URL format.
Solution 2
You have to use RedirectResponse instead of Response
use Symfony\Component\HttpFoundation\RedirectResponse;
And then:
return new RedirectResponse('http://your.location.com');
Related videos on Youtube
![Saman Mohamadi](https://i.stack.imgur.com/O5hAg.jpg?s=256&g=1)
Author by
Saman Mohamadi
Updated on June 18, 2020Comments
-
Saman Mohamadi about 4 years
How can I redirect to an external URL within a symfony action?
I tried this options :
1- return $this->redirect("www.example.com");
Error : No route found for "GET /www.example.com"
2- $this->redirect("www.example.com");
Error : The controller must return a response (null given).
3- $response = new Response(); $response->headers->set("Location","www.example.com"); return $response
No Error but blank page !
-
M Khalid Junaid about 9 yearsFirst one should work also can you specify which version you are using
-
Saman Mohamadi about 9 yearssymfony/symfony v2.6.6
-
-
Artamiel about 9 yearsWhat you've provided is redirect to existing URL in application. If you look closely at OP's title, you will notice that he is asking about external Url. Take a look at my answer.
-
kba about 9 yearsYou are right. I have badly copy an example from official doc.
-
beterthanlife over 8 yearsThis was the only way I could get symfony to redirect in such a way that the browser url changed (for internal redirection).
-
Artamiel over 8 years@beterthanlife I'm glad it was helpful to you.
-
David Soussan over 8 yearsThis does NOT answer the question, which is to redirect to an EXTERNAL url
-
kba over 8 yearsI'm sorry but I think
http://stackoverflow.com
is an EXTERNAL URL :). Can you explain more deeply why do you think it's bad answer? -
Will B. almost 8 yearsThis answer calls the Symfony2
Controller::redirect
method, which is the equivalent of callingnew RedirectResponse
when navigating to a symfony route. github.com/symfony/framework-bundle/blob/2.8/Controller/… Both of which are the same as specifyingheader('Location: http://www.example.com');
Effectively the issue was thathttp://
was not included in the OP's question, as allRedirectResponse
does is extendResponse
and calls$this->headers->set('Location', $url)
and setting the content to include ameta refresh
as the content body. -
kba almost 8 years@fyrye I still don't understand what's the point :). Yes, the issue was about
http://
because without that you get redirect to urlhttp://mysfsite/www.example.com
instead ofhttp://www.example.com
. And this is reason of errorNo route found for "GET /www.example.com"
. It's explained in my answer. I still don't understand what's wrong with my answer. Your reaction is not answer for me on @David Soussan's comment. -
Will B. almost 8 years@kba I was explaining to the less familiar that may view your answer that both answers given and the OP's methods would have the same results. You provided the correct answer in bold, where an absolute URL (the protocol
http://
orhttps://
) is required in order to redirect the response correctly. Some may not know that your answer requires it to be called from within aController
instance. I did not downvote or upvote, just provided context for others. For example someone trying to use$this->redirect()
from within aCommand
orEntity
instance. -
kba almost 8 years@fyrye Oh ok. I thought you are reacting on David Soussan comment. My bad :).
-
Jamshad Ahmad almost 6 years@Artamiel thanks man. This one worked for external links. Selected Answer did not work for me for some reason.