Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jlin1010's avatar

Redirect to external URL in controller not working

Hi All,

I am new to Laravel so I might be missing something really simple. I cannot seem to get redirect to work in a controller to send user to an external website.

For example, in a controller I have a function:

public function goToGoogle() { $external_uri = 'https://www.google.com'; return redirect($external_uri); }

This gives me a blank page when goToGoogle() function runs (stays on the local route URI that initiated the controller function).

While, below works in the web.php routes page: Route::get('/test', function() { return redirect('https://www.google.com');});

if I go to localhost/test, it redirects me to https://www.google.com.

I am not sure what I am missing. Thank you in advance for any advice/feedback you might have.

John

0 likes
13 replies
jlin1010's avatar

Actually, I just found an answer on stackoverflow.

I needed to do:

redirect()->to($external_uri)->send();

This worked.

3 likes
M_rhnm's avatar

@jlin1010 i have a problem like you and try any ways but fixed the bug with this solution

pcabral's avatar

Why are you redirect the user inside the controller ? What is the action that call the controller function ?

jlrdw's avatar

Or just put a regular Link in blade, nothing special needed.

jlin1010's avatar

Thank you all for your feedback!

I have tried "return redirect()->away('https://www.google.com');" but that did not work for me for some reason (simply shows a blank page). Sorry, I am really new to Laravel, so perhaps I am not doing things correctly.

I needed to redirect the user to a third party authentication/authorization in a JWT workflow. Should I perhaps do the redirect in a view instead of in the controller? The controller basically pulls together the necessary URI parameters (client ID, return uri, etc) from env variable, then redirects the user to that URI for authentication.

fylzero's avatar

@jlin1010 I've just tested the following code and it works correctly in the latest version of Laravel. What version are you running?

Route::get('/google', 'TestController@google');
public function google()
{
    return redirect()->away('https://www.google.com');
}
2 likes
jlin1010's avatar

That's a good question. I am running 5.8.36. I will check with my colleague to see if we can/should upgrade to 6.

jlin1010's avatar

@fylzero Thank you for the feedback again. I made the route and controller you posted and that worked... I will do a little more digging to see why it's not working in the controller I am working on. Thanks again!

fylzero's avatar

@jlin1010 Please mark a best answer if this helped. I'd appreciate it. ...and np! Goodluck.

guspecht's avatar

You said that you have a function on your Controller goToGoogle() so probably you are missing a return that's all.

Example: This is what I think you are doing, you are calling the test function in this case.

class UserController extends Controller { public function goToGoogle() { $external_uri = 'https://www.google.com'; return redirect($external_uri); }

public function test()
{
	$this->goToGoogle();
}

}

As you said you were getting a blank page, this is what you need to do,:

class UserController extends Controller{ public function goToGoogle() { $external_uri = 'https://www.google.com'; return redirect($external_uri); }

public function test(){
	return $this->goToGoogle();
}

}

on test() I added the return as well, I think that's why you are getting the blank page and staying in the same page/url, you are just missing a return to return the function's return :)

Please or to participate in this conversation.