1

I have on my site link to download some file:

<a href="documents/myfile.pdf">Download</a>

However, when I click on the link, browser does not download this file automatically, but redirects me to file url http://example.com/documents/myfile.pdf where I see its content. I only know it's some issue with my .htaccess file, but I have no clue what it could be. I use .htaccess from html5boilerplate project, you can preview the source here: https://raw.githubusercontent.com/h5bp/html5-boilerplate/master/dist/.htaccess. I appreciate the help.

Indy
  • 4,838
  • 7
  • 24
  • 35

4 Answers4

1

The best way to force the download a file using htaccess is here. I provide a fully working example, put all files inside root and test them :)

.htaccess

RewriteEngine on
RewriteRule ^(.*).(txt|pdf|zip|rar)$ /download.php?file=$1.$2 [R,L]

download.php

<?php
  if (!empty($_GET['file'])) {
    $file = basename($_GET['file']);
    $type = array("txt", "pdf", "zip", "rar");
    $exts = strtolower(substr(strrchr($file, "."), 1));
    if (!in_array($exts, $type)) {
      header("HTTP/1.0 403 Forbidden");
      exit('File not allowed!');
    } else {
      if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . $file . '"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: Keep-Alive');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . sprintf("%u", filesize($file)));
        $fh = fopen($file, "rb");
        while (!feof($fh)) {
          echo fgets($fh);
          flush();
        }
        fclose($fh);
        exit;
      } else {
        header("HTTP/1.0 404 Not Found");
        exit('File not found!');
      }
    }
  }
?>

link_page.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Download Page</title>
<link rel="stylesheet" type="text/css" href="link_page.css">
</head>
<body>
<div class="mainbox">
  <div class="box1">
    <span class="title">Download Page</span>
  </div>
  <div class="cleardiv"></div>
  <div class="sbar"></div>
  <div class="cleardiv"></div>
  <div class="box2">
    <span class="row">File: <a href="file.zip" class="link">file.zip</a> Click to download file.zip</span><br />
    <span class="row">File: <a href="file.rar" class="link">file.rar</a> Click to download file.rar</span><br />
    <span class="row">File: <a href="file.txt" class="link">file.txt</a> Click to download file.txt</span><br />
  </div>
  <div class="cleardiv"></div>
</div>
</body>
</html>

link_page.css

@import url(http://fonts.googleapis.com/css?family=Oswald);
html {
  display: table;
}
html, body {
  width: 100%;
  height: 100%;
}
body {
  color: black;
  display: table-cell;
  vertical-align: middle;
  background-color: lightgray;
  font-family: Oswald, 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 14px;
  font-style: normal;
  line-height: normal;
  font-weight: normal;
  font-variant: normal;
}
.mainbox {
  border-radius: 7px;
  border: 1px solid gray;
  background-color: darkgray;
  width: 400px;
  min-height: 100px;
  padding: 10px;
  margin: 0 auto;
  margin-top: 50px;
  margin-bottom: 50px;
}
.box1 {
  vertical-align: middle;
  text-align: center;
  margin: 0 auto;
  padding: 10px;
}
.box2 {
  vertical-align: middle;
  text-align: center;
  margin: 0 auto;
  padding: 10px;
}
.title {
  font-family: Oswald, 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 19px;
  font-style: normal;
  line-height: normal;
  font-weight: normal;
  font-variant: normal;
  vertical-align: middle;
  text-align: center;
  margin: 0 auto;
  padding: 10px;
}
.row {
  font-family: Oswald, 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 14px;
  font-style: normal;
  line-height: normal;
  font-weight: normal;
  font-variant: normal;
  vertical-align: middle;
  text-align: left;
  float: right;
  width: 295px;
  margin: 0 auto;
  padding: 10px;
}
a.link:link {
  color: #E6E6FA;
  text-decoration: none;
  background-color: #006699;
  border: black 1px solid;
  border-radius: 5px;
  padding: 4px;
}
a.link:visited {
  color: #E6E6FA;
  text-decoration: none;
  background-color: #006699;
  border: black 1px solid;
  border-radius: 5px;
  padding: 4px;
}
a.link:hover {
  color: #E6E6FA;
  text-decoration: none;
  background-color: #003399;
  border: black 1px solid;
  border-radius: 5px;
  padding: 4px;
}
.cleardiv {
  clear: both;
}
.sbar {
  width: 90%;
  border-top: 1px solid gray;
  text-align: center;
  vertical-align: middle;
  margin: 0 auto;
}

download my example and test it

Alessandro
  • 900
  • 12
  • 23
0

You can try :

<FilesMatch "\.(gz|pdf|zip|rar)$" >
    Order allow,deny
    Allow from all
    Satisfy any
</FilesMatch>
Pixel
  • 1,946
  • 2
  • 15
  • 26
0

Maybe you can use HTML5 download attribute: http://davidwalsh.name/download-attribute

Possible duplicate of: (HTML) Download a PDF file instead of opening them in browser when clicked

Community
  • 1
  • 1
Pieter21
  • 1,765
  • 1
  • 10
  • 22
0

Solution:

<FilesMatch "\.(txt|pdf|zip|rar)$">

  ForceType application/octet-stream
  Header add Content-Disposition "attachment"

  Order allow,deny
  Allow from all
  Satisfy any

</FilesMatch>
Indy
  • 4,838
  • 7
  • 24
  • 35