Commands.page Logo

How to Set Maximum Redirects in Wget on Ubuntu

This article provides a concise guide on limiting HTTP redirects when using the wget tool on Ubuntu Linux. It covers the specific command-line flag needed to cap redirection attempts, explains the default behavior, and offers examples to prevent infinite loops or excessive requests during downloads.

Using the –max-redirect Flag

To specify a maximum number of redirects in wget, use the --max-redirect option followed by the desired number. This tells wget to stop following links after the specified count is reached.

The basic syntax is:

wget --max-redirect=NUMBER [URL]

Replace NUMBER with the integer limit you wish to enforce. For example, to allow only 5 redirects before aborting, run:

wget --max-redirect=5 http://example.com

Default Behavior and Disabling Redirects

By default, wget allows up to 20 redirects. If a server attempts to redirect the request more than this limit, wget will terminate the connection with an error.

If you want to disable redirects entirely, set the value to 0. This ensures wget downloads only the initial URL provided without following any location headers:

wget --max-redirect=0 http://example.com

Why Limit Redirects?

Setting a limit is useful for scripting and automation. It prevents your system from getting stuck in redirect loops caused by server misconfigurations. It also saves bandwidth and time by failing fast when a resource is not located at the expected path.