Prevent Following Redirects with wget

Prevent Following Redirects with wget

By default, wget follows redirects automatically. This behavior is often convenient, but there are situations where you might want to avoid following redirects. For example, you might be debugging a website or want to capture only the initial response from a URL. This tutorial demonstrates how to prevent following redirects with wget.

To prevent wget from following redirects, use the --max-redirect option and set its value to 0. Here's an example:

wget --max-redirect=0 https://httpbin.org/relative-redirect/1

When you run the command, you might see output like this:

--2025-01-17 18:48:02--  https://httpbin.org/relative-redirect/1
Resolving httpbin.org (httpbin.org)... 50.19.58.113, 3.210.94.60
Connecting to httpbin.org (httpbin.org)|50.19.58.113|:443... connected.
HTTP request sent, awaiting response... 302 FOUND
Location: /get [following]
0 redirections exceeded.

This output indicates that wget encountered a 302 redirect but stopped because it was configured not to follow it. The Location header shows the next URL in the chain, which you can choose to follow manually if needed.

Leave a Comment

Cancel reply

Your email address will not be published.