Stop Laravel's HTTP Client from Following Redirects

July 7th, 2024 • 1 minute read time

By default, Laravel's HTTP client follows redirects. You can turn this off for individual requests or globally. Here's how.

To turn off redirect follows for single requests, use the withoutRedirecting method and chain:

$response = Http::withoutRedirecting()
	->get('http://example.com');

Behind the scenes, this sets the allow_redirects option in Guzzle to false. Laravel wraps Guzzle for its HTTP client, so you'll find many more methods that do the same thing — they just set options on the fly.

If you want to globally disable redirects with Laravel's HTTP client, set this somewhere globally, like your AppServiceProvider, inside the boot method:

public function boot(): void
{
    Http::globalOptions([
        'allow_redirects' => false,
    ]);
}

Once again, this sets the Guzzle allow_redirects option to false, but for all requests.

If you found this article helpful, you'll love our practical screencasts.
Author
Alex Garrett-Smith
Share :

Comments

No comments, yet. Be the first!