doujiunai2169
doujiunai2169
2015-02-08 13:41

仅为php用户htaccess下载文件

how could i do this with htaccess , please ?

if (url has not parameter ?down=true) 
redirect back
else
do noting
end if

example

example.com/upload/file.pdf // it mustn't downlaod the file

example.com/upload/file.pdf?down=true // it must download the file

please help me :)

that's my htaccess file

RewriteEngine on
RewriteRule ^upload/file/(.*) http\:\/\/www\.example\.org\/down.php?file=$1 [L]

this is the down.php page

<?php
session_start();

if(isset($_SESSION["user_id"])){ 
if(isset($_GET["file"])) $file = mysql_escape_string($_GET["file"]);
$file = "http://www.example.org/upload/file/".$file;
$file = preg_replace("/ /", "%20", $file);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//header('Content-Length: ' . filesize($file));
readfile($file);
exit;

}else{
header('Location:login.php');
}
exit();
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douji5746 douji5746 6年前

    You need %{QUERY_STRING} variable inside a RewriteCond for that checking for redirect like this.

    RewriteEngine On
    RewriteCond %{QUERY_STRING} !(^|&)down=true($|&)
    RewriteRule example\.com\/upload\/file\.pdf /index.php [L]
    

    if your RewiteCond has a down=true in the query string, then do not apply the following rule,else rewrites all request to /index.php

    点赞 评论 复制链接分享