{"id":331,"date":"2024-11-15T14:46:01","date_gmt":"2024-11-15T21:46:01","guid":{"rendered":"https:\/\/blog.d5.ca\/?p=331"},"modified":"2026-01-12T15:52:00","modified_gmt":"2026-01-12T22:52:00","slug":"force-files-to-download-rather-than-open-in-browser","status":"publish","type":"post","link":"https:\/\/blog.d5.ca\/?p=331","title":{"rendered":"Force Files to Download rather than Open in browser"},"content":{"rendered":"<p>There are certain circumstances where it&#8217;s desirable to force files hosted on an Apache web server to download rather than rendering in the browser. This can be accomplished using the following .htaccess file&#8230;<!--more--><\/p>\n<pre>&lt;FilesMatch \".+\\.*$\"&gt;\r\n  SetHandler !\r\n  ForceType application\/octet-stream\r\n&lt;\/FilesMatch&gt;<\/pre>\n<p>The <em>SetHandler !<\/em> line prevents server-side execution of file types such as .php and .cgi.<\/p>\n<p>The\u00a0<em>ForceType&#8230;<\/em> line causes the browser to treat files such as .pdf and .html as non-renderable, thus initiating a download.<\/p>\n<h3>References:<\/h3>\n<p><a href=\"https:\/\/www.myguysolutions.com\/2015\/03\/04\/force-files-to-download-and-not-open-in-browser-using-apache-or-php\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.myguysolutions.com\/&#8230;<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/16105412\/selectively-force-file-download-with-apache-based-on-url-parameter\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/&#8230;<br \/>\n<\/a><a href=\"https:\/\/forum.howtoforge.com\/threads\/how-to-disable-php-fpm-only-for-a-subfolder.87153\/\" target=\"_blank\" rel=\"noopener\">https:\/\/forum.howtoforge.com\/&#8230;<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are certain circumstances where it&#8217;s desirable to force files hosted on an Apache web server to download rather than rendering in the browser. This can be accomplished using the following .htaccess file&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[25],"tags":[53,13,15],"class_list":["post-331","post","type-post","status-publish","format-standard","hentry","category-reference","tag-apache","tag-php","tag-web-development"],"_links":{"self":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts\/331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=331"}],"version-history":[{"count":2,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions"}],"predecessor-version":[{"id":333,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions\/333"}],"wp:attachment":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}