【问题标题】:How to allow all file to accessible from uploads folder through .htaccess setting如何允许通过 .htaccess 设置从上传文件夹访问所有文件
【发布时间】:2017-04-01 15:47:14
【问题描述】:

我有文件的根目录

 - api.php
 - .htaccess
 - uploads/
 - uploads/abc.jpg
 - uploads/xyz.jpg

我正在为移动开发者编写 REST API,所以我已经成功创建了下面列出的 API 数量

api.php

function register(){
    //POST Method
    //I can access this API with URL http://example.com/register
}

function login(){
    //POST Method
    //I can access this API with URL http://example.com/login
}

function user(){
    //GET Method
    //I can access this API with URL http://example.com/user
}

现在的问题是上传目录中存储的用户个人资料图像,即(ab.jpg,xyz.jpg),我无法使用http://example.com/uploads/abc.jpg 访问它

我发现这是由于我已将.htaccess 文件放在根级目录中,该文件中的代码是:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-s
    RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*)$ api.php [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -s
    RewriteRule ^(.*)$ api.php [QSA,NC,L]   
</IfModule>

我不知道如何允许上传文件夹使用 .htaccess 公开访问它

【问题讨论】:

  • 这是因为您将现有目录重写为 /api.php 。尝试删除您的 #2 RewriteCond 和规则。
  • 并将 !-s 替换为 !-f 以避免重写现有文件。
  • 谢谢@starkeen,这就是我想要的

标签: php apache .htaccess mod-rewrite


【解决方案1】:

希望这会奏效。只需将此行添加到每个重定向。

RewriteCond %{REQUEST_FILENAME} !\.(jpg|png)$

Check .htaccess here

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !\.(jpg|png)$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} !\.(jpg|png)$
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*)$ api.php [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} !\.(jpg|png)$
    RewriteCond %{REQUEST_FILENAME} -s
    RewriteRule ^(.*)$ api.php [QSA,NC,L]   
</IfModule>

【讨论】:

  • 不,它现在可以工作,当我用我的代码图像 URL 替换您的代码时,API URL 不起作用,当我同时放置两者时,它不起作用
  • @AmanMaurya 更新了我的帖子,您可以再次查看,如果您有任何问题,请告诉我
猜你喜欢
  • 2016-11-27
  • 2014-12-31
  • 1970-01-01
  • 2022-06-28
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
  • 2013-07-31
相关资源
最近更新 更多