2016-07-29 72 views
0

我不希望我的網站在我的網站空間的根文件夾! 這樣做的目的是創建一個文件夾,是不是通過網絡訪問(如安全文件,下載)重定向整個網站到子文件夾

我試圖重定向,但我不讓它工作100%

例子:

-root 
--downloads 
--web 
---f1 
--.htaccess 

我htaccess文件:

<ifmodule mod_rewrite.c> 
    RewriteEngine On 
    RewriteRule ^(.*)$ /web/$1 [L,NC] 
</ifmodule> 

這個工作得很好url.com被重定向ŧ o url.com/web而不更改地址欄中的網址。

但這裏的問題,當我嘗試訪問像url.com/f1它被重定向到(和顯示)url.com/web/f1一個文件夾開始 ,我不想將顯示在地址欄中。

此外,我仍然可以訪問url.com/downloads我希望被重定向到url.com/web/downloads

可有人請向我解釋熱解決這一問題,或者是什麼實現這一目標的正確方法。

+0

不應該'downloads'或'f1'等文件夾是下'網/'? – anubhava

回答

0

這樣做的目的是創建一個文件夾是無法訪問通過 網絡(如安全文件,下載)

所以對於你的第一個問題,您需要將您的文件夾根外。這是讓他們安全的最佳方式。像這樣的結構。

--downloads 
--f1 
--root 
    -- web 

然後,他們絕對沒有辦法在瀏覽器中達到它,並且你不需要爲此重寫文件。

現在您可以使用後端代碼來檢索文件,然後允許用戶在授權後下載它。你沒有說你在用什麼,所以我會提供一個PHP例子。

然後,您可以使用這樣的代碼強制下載。你可以使用下面的鏈接作爲完整的例子,也可以使用數據庫。

<?php 

ignore_user_abort(true); 
set_time_limit(0); // disable the time limit for this script 

$path = "/home/username/downloads/"; // change the path to fit your websites document structure 

$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation 
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters 
$fullPath = $path.$dl_file; 

if ($fd = fopen ($fullPath, "r")) { 
    $fsize = filesize($fullPath); 
    $path_parts = pathinfo($fullPath); 
    $ext = strtolower($path_parts["extension"]); 
    switch ($ext) { 
     case "pdf": 
     header("Content-type: application/pdf"); 
     header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download 
     break; 
     // add more headers for other content types here 
     default; 
     header("Content-type: application/octet-stream"); 
     header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); 
     break; 
    } 
    header("Content-length: $fsize"); 
    header("Cache-control: private"); //use this to open files directly 
    while(!feof($fd)) { 
     $buffer = fread($fd, 2048); 
     echo $buffer; 
    } 
} 
fclose ($fd); 
exit; 

http://www.web-development-blog.com/archives/php-download-file-script/

相關問題