3

In a Spring Controller Action, I am using the following statement to redirect to an external URL:

String redirectUrl = "www.yahoo.com";
return "redirect:" + redirectUrl;

However, it appears that it's redirecting the url locally and not replacing the entire address bar URL with www.yahoo.com.

Ex: With the above redirection, my address bar now looks like:

http://localhost/myApp/auth/www.yahoo.com

How do I resolve this? I even tried redirecting to a view and then having the view redirect the URL, but still the same result. The only way it seems to work is if I have http://www.yahoo.com or https://www.yahoo.com But I wanted it to redirect the URL as specified and not necessarily mention the protocol. Ex: yahoo.com is similiar to http://www.yahoo.com if you go directly in the address bar.

Thanks

Jake
  • 25,479
  • 31
  • 107
  • 168

1 Answers1

11

The protocol is required if the host is different to that of the current host

String redirectUrl = "http://www.yahoo.com";
return "redirect:" + redirectUrl;

Have a look at the redirect: prefix section from Spring Web MVC framework

A logical view name such as redirect:/myapp/some/resource will redirect relative to the current Servlet context, while a name such as redirect:http://myhost.com/some/arbitrary/path will redirect to an absolute URL.

Reimeus
  • 158,255
  • 15
  • 216
  • 276