2015-02-08 32 views
0

我怎麼能用htaccess來做這件事?只爲php用戶下載文件htaccess

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

例如

example.com/upload/file.pdf //它不能立即下載該文件

example.com/upload/file.pdf?down=true //它必須下載文件

請幫我:)

這是我的htaccess文件

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

這是down.php頁

<?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(); 

回答

0

你需要一個RewriteCond%{QUERY_STRING}變量對於檢查重定向這樣。

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

如果您RewiteCond已在查詢字符串down=true,那麼不適用下列規則,否則,重寫所有請求/index.php

+0

感謝您的回覆,但你可以寫完整的條件,我 因爲我失敗了 – 2015-02-08 14:25:46