Protecting digital downloads in WooCommerce
Selling an e-book, a PDF, music or software through WooCommerce? Then you want only paying customers to be able to get the file. WooCommerce handles this with a protected download link, but that only works properly if the download method is secure and the folder holding the files is locked down. Here you can read how it works, what to configure and how to check that a direct link doesn't work.
How a download works in WooCommerce
On a product, tick Downloadable and upload the file. WooCommerce puts it in the folder wp-content/uploads/woocommerce_uploads/. By default it appends a random string of characters to the file name, so the name is hard to guess. The customer never gets that address, but a link to your shop instead. On every click, WooCommerce checks whether the download belongs to a paid order and whether the limit and the expiry date haven't been reached yet. Only then does the customer get the file.
If nothing is shipped by post, also tick Virtual. That way WooCommerce won't charge shipping costs.
Choose a secure download method
Go to WooCommerce > Settings > Products > Downloadable products and look at the File download method:
| Method | What happens | Secure? |
|---|---|---|
| Force downloads (default) | PHP reads the file and sends it to the customer itself. | Yes. With very large files, PHP may hit its time limit. |
| X-Accel-Redirect/X-Sendfile | PHP does the check, the web server sends the file. | Yes, if the web server supports it. |
| Redirect only | The customer is sent to the actual address of the file. | No. Anyone who has the address can share it. WooCommerce itself calls it insecure. |
Choose Force downloads, unless you're sure X-Sendfile works. Also leave the option that makes WooCommerce fall back to redirecting as a last resort switched off.
The woocommerce_uploads folder must be locked down
With a secure method, the folder itself must be closed to regular visitors. Otherwise someone who knows the file name, for example from a leaked link, can fetch the file directly. That's why WooCommerce places two files in the folder during installation:
- an empty
index.html, so no list of files can be seen; - a
.htaccesswith the ruledeny from all, which refuses all direct requests.
Such a .htaccess file works on web servers that read it, such as Apache and LiteSpeed. Our servers run on LiteSpeed, so at WebOké this protection works without any extra settings. On a server running nginx, the web server doesn't read .htaccess; there a rule in the server configuration is needed. If you later move your shop to such a server, check it again.
The .htaccess file is hidden. To see whether it's there, see Showing and editing hidden files such as .htaccess. Don't delete it.
Checking that a direct link doesn't work
- On a downloadable product, open the file address as WooCommerce shows it. It starts with
/wp-content/uploads/woocommerce_uploads/. - Open that address in a private window where you're not logged in.
- A 403 Forbidden error (or 401 or 404) is good. If you can simply view or download the file, the folder is open.
Always test a real file, not just the folder. A blank page for the folder may be the empty index.html, while the files themselves are still accessible. You can also do it from the command line: curl -I https://example.com/wp-content/uploads/woocommerce_uploads/bestand.pdf should return a 403. You can read what a 403 means in Forbidden (403): You don't have permission to access / on this server.
Limits and access
- You set the Download limit and Download expiry per product or variation. Empty means unlimited. A strict limit restricts sharing, but leads to complaints when someone buys a new computer.
- Access after payment is on by default: the customer can download as soon as the payment has succeeded.
- Approved download directories: only files from these folders or addresses may be used as downloads. Keep the list short.
If you sell digital content to consumers, also arrange how the right of withdrawal works for downloads. WooCommerce doesn't do this automatically. You'll find an explanation in the free course A webshop with WooCommerce, lesson 5.2 The right of withdrawal: 14 days and the model form. This article is not legal advice; check with your country's consumer authority (in the Netherlands, the ACM) or ask a lawyer.
Further reading
- Digital and downloadable product handling (WooCommerce, English)
- Free WordPress course, lesson 7.4 Hardening the site: permissions, headers and security plugins
Want to learn more? Take the free course A webshop with WooCommerce, lesson 3.3 Digital and virtual products.
