2012-07-23 107 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文件的顶部修复该问题。