0

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

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

2 回答 2

2

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

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

和:

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

并将条件和规则放在任何index.php路由规则之前。

于 2012-07-23T21:10:21.410 回答
0

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

工作规则:

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

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

于 2012-07-23T21:42:02.500 回答