How to Convert wget Links for Offline Viewing?

Downloading a website for local browsing requires updating the internal links so they point to local files instead of the internet. The wget command-line utility provides a built-in feature to handle this automatically during or after the download process. By using the correct combination of flags, you can convert absolute and relative URLs into working local paths, ensuring a seamless offline reading experience.

The Essential Command Flags

To successfully download a webpage or an entire site and convert its links for offline use, you rely on a few specific wget options.

Practical Examples

Downloading a Single Page for Offline Viewing

If you only need a single article or webpage with all its styles and images intact, use the following command:

wget -p -k https://example.com/page.html

In this command, -p grabs the images and CSS, while -k ensures that the HTML file updates its paths to find those downloaded images and stylesheets on your local drive.

Mirroring an Entire Website

To clone an entire website for complete offline navigation, the --mirror flag is often combined with link conversion:

wget --mirror --convert-links --page-requisites https://example.com/

Note: The --mirror flag is a shortcut for infinite recursion, time-stamping, and keeping directory listings. When paired with --convert-links, it creates a fully browsable local replica of the target site.

The link conversion process happens at the very end of the download session. While wget is actively downloading files, it keeps track of what it has saved and where. Once the entire download queue is finished, it revisits the downloaded HTML files and rewrites the URLs.

For instance, an image tag originally pointing to an online source:

<img src="https://example.com/images/logo.png">

Will be rewritten to point to your local directory structure:

<img src="images/logo.png">

This behavior ensures that you can open the root HTML file in any browser without an active internet connection, and the page will render exactly as it did online.