我有这样的情况。 我正在 Zend Framework 和 htaccess 中开发应用程序,将每个请求指向 索引.php。如果请求路径上存在某些文件或目录,则 htaccess 允许访问此文件,如 css、js、图像等...
现在我有这样的链接:
example.com/profile/martin-slicker-i231
对于 Zend Router,它指向 Controller 帐户和操作 View 配置文件。我想为我的用户添加一些头像,我在公共(public)文件夹中创建了目录(这样图像就可以像 css 和 js 一样被服务器访问)。该目录名为“profile”,其中的子目录为“martin-slicker-i231”。服务器可以看到所有路径:
public/
.htaccess
index.php
profile/
martin-slicker-i231/
avatar-small.jpg
问题是当我将浏览器指向 example.com/profile/martin-slicker-i231 时 将我指向此目录而不是 Controller 和操作。当我与用户一起删除文件夹时,流程将转到 Controller 和操作。如何配置 .htaccess 以便 example.com/profile/martin-slicker-i231 将指向 Controller 和操作,但请求 example.com/profile/martin-slicker-i231/avatar-small.jpg 指向文件。这是我的 .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
有人可以帮忙吗?
最佳答案
只需删除说明它应该为目录提供服务的部分:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
现在它将直接提供文件(大小 > 0)和符号链接(symbolic link),但会将目录引用和其他 URL 发送到您的应用程序
关于php - htaccess 阻止访问目录但允许访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6545155/