.htaccess rule to prompt file download

Here’s a quick .htaccess trick you can use to make a user’s browser prompt them to download an image rather than the image just opening in the browser.

RewriteEngine On
RewriteCond %{QUERY_STRING} =download
RewriteRule ([^/]*)$ - [L,E=download:$1]
Header onsuccess set Content-disposition "attachment; filename=%{download}e" env=download

What this does is that any links to images (or any other file) will be handled normally, but if you add ‘?download’ to the end of your link, the user will be prompted to download the file instead, e.g.

Normal link <a href="/filepath/some-image.jpg" >Link</a>
Download link <a href="/filepath/some-image.jpg?download" >Link</a>

Continue reading

Posted on by xoogu, last updated