2012-07-23 64 views
0

我一直在尋找所有,我找不到適合我的解決方案。.htaccess重寫直接文件訪問

我想重定向http://example.com/files/file.ext - >http://example.com/users/documents/file.ext

無論我怎麼努力,當我直接上去的時候,它下載文件。該文件的GET請求也不會顯示在我的任何apache日誌中。我登錄了調試。

[編輯] 我試圖下載的文件有各種類型,包括ppt,pdf,xls,zip,doc等。我想將文件名重新寫入新URI的末尾。我也使用CodeIgniter,因此/ users/documents /是一個RESTy URI。

任何人都有修復?

這裏是我的.htaccess文件:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    Options +FollowSymLinks 
    RewriteBase/


    #Removes access to the system folder by users. 
    #Additionally this will allow you to create a System.php controller, 
    #previously this would not have been possible. 
    #'system' can be replaced if you have renamed your system folder. 
    RewriteCond %{REQUEST_URI} ^system.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 

    #When your application folder isn't in the system folder 
    #This snippet prevents user access to the application folder 
    #Submitted by: Fabdrol 
    #Rename 'application' to your applications folder name. 
    RewriteCond %{REQUEST_URI} ^application.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 

    #Checks to see if the user is attempting to access a valid file, 
    #such as an image or css document, if this isn't true it sends the 
    #request to index.php 


    <IfModule mod_php5.c> 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^(.*)$ index.php?/$1 [N] 

     RewriteCond %{REQUEST_URI} ^/files/ [NC] 
     RewriteRule ^/files/(.*)$ /users/documents/$1 [L,R=301] 


    </IfModule> 

    <IfModule !mod_php5.c> 
     RewriteRule ^(.*)$ index.php?/$1 [L] 
    </IfModule> 

</IfModule> 
+0

你有沒有試過把你的'/ files /'規則移到重寫'index.php'上面的規則? – 2012-07-23 20:34:10

+0

我剛試過。我以前有過,但沒有那個確切的規則。它仍然只是下載文件。 – Scone 2012-07-23 20:59:37

+0

你是什麼意思「只是下載文件」。它是HTML,但沒有呈現?你有分配給擴展名「.ext」的處理程序嗎? – 2012-07-23 21:00:59

回答

2

如果這是在你的htaccess文件,請嘗試更換這行:

RewriteRule ^/files/(.*)$ /users/documents/$1 [L,R=301] 

有:

RewriteRule ^/?files/(.*)$ /users/documents/$1 [L,R=301] 

,並把該條件和規則在任何index.php路由規則之前。

+0

功能沒有變化。我將條件和修改後的重寫規則移到了「RewriteBase /」下面 – Scone 2012-07-23 21:21:07

+1

@Scone可能條件失敗了? 'RewriteCond%{REQUEST_URI} ^/files/[NC]'對我來說看起來很好,也許還會把'?'加到前面的斜槓上(儘管它不需要它),所以它是'^ /?files /'? – 2012-07-23 21:24:16

+0

我會嘗試。 ''是什麼?做? – Scone 2012-07-23 21:27:44

0

我去了http://martinmelin.se/rewrite-rule-tester/並測試了我的重寫規則。它說RewriteCond不匹配任何東西,但是當我刪除它匹配的規則時。

工作規則:

RewriteRule ^/?files/(.*)$ /users/documents/$1 [L,R=301] 

移動這對htaccess文件的頂部修復該問題。