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