.htaccess重写直接文件访问

时间:2012-07-23 20:30:12

标签: .htaccess mod-rewrite

我一直在寻找,我找不到适合我的解决方案。

我正在尝试重定向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>

2 个答案:

答案 0 :(得分:2)

如果这是在您的htaccess文件中,请尝试替换此行:

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

使用:

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

将条件和规则置于任何index.php路由规则之前。

答案 1 :(得分:0)

我去了http://martinmelin.se/rewrite-rule-tester/并测试了我的重写规则。 它说RewriteCond没有匹配任何东西,但当我删除匹配的规则时。

工作规则:

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

将其移至htaccess文件的顶部可解决此问题。