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');
Share:
68,488

Related videos on Youtube

Saman Mohamadi
Author by

Saman Mohamadi

Updated on June 18, 2020

Comments

  • Saman Mohamadi
    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
      M Khalid Junaid about 9 years
      First one should work also can you specify which version you are using
    • Saman Mohamadi
      Saman Mohamadi about 9 years
      symfony/symfony v2.6.6
  • Artamiel
    Artamiel about 9 years
    What 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
    kba about 9 years
    You are right. I have badly copy an example from official doc.
  • beterthanlife
    beterthanlife over 8 years
    This was the only way I could get symfony to redirect in such a way that the browser url changed (for internal redirection).
  • Artamiel
    Artamiel over 8 years
    @beterthanlife I'm glad it was helpful to you.
  • David Soussan
    David Soussan over 8 years
    This does NOT answer the question, which is to redirect to an EXTERNAL url
  • kba
    kba over 8 years
    I'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.
    Will B. almost 8 years
    This answer calls the Symfony2 Controller::redirect method, which is the equivalent of calling new RedirectResponse when navigating to a symfony route. github.com/symfony/framework-bundle/blob/2.8/Controller/… Both of which are the same as specifying header('Location: http://www.example.com'); Effectively the issue was that http:// was not included in the OP's question, as all RedirectResponse does is extend Response and calls $this->headers->set('Location', $url) and setting the content to include a meta refresh as the content body.
  • kba
    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 url http://mysfsite/www.example.com instead of http://www.example.com. And this is reason of error No 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.
    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:// or https://) is required in order to redirect the response correctly. Some may not know that your answer requires it to be called from within a Controller instance. I did not downvote or upvote, just provided context for others. For example someone trying to use $this->redirect() from within a Command or Entity instance.
  • kba
    kba almost 8 years
    @fyrye Oh ok. I thought you are reacting on David Soussan comment. My bad :).
  • Jamshad Ahmad
    Jamshad Ahmad almost 6 years
    @Artamiel thanks man. This one worked for external links. Selected Answer did not work for me for some reason.