htaccess 阻止访问目录但允许访问文件 [英] htaccess block access to directory but allow access to files

查看:28
本文介绍了htaccess 阻止访问目录但允许访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的情况.我正在 Zend Framework 中开发应用程序,并且 htaccess 将每个请求指向索引.php.如果请求路径上存在某个文件或目录,则 htaccess 允许访问这些文件,如 css、js、图像等...

I have such situation. I'm developing application in Zend Framework and htaccess pointing every request to index.php. If some file or directory exists on the request path then htaccess allow access to this files like css, js, images etc...

现在我有这样的链接:

example.com/profile/martin-slicker-i231

使用 Zend 路由器,它指向控制器帐户和操作视图配置文件.我想为我的用户添加一些头像,并在公共文件夹中创建了目录(因此图像可以被服务器访问,如 css 和 js).该目录名为profile",其中的子目录为martin-slicker-i231".服务器可以看到所有路径,如下所示:

With Zend Router it points to controller account and action viewprofile. I want to add some avatart for my users and I created directory in public folder (so the images would be accessible by the server like css and js). The directory is named "profile" and subdirectory in it is "martin-slicker-i231". All path is visible by server like that:

public/
  .htaccess
  index.php
  profile/
    martin-slicker-i231/
      avatar-small.jpg

问题是当我将浏览器指向 example.com/profile/martin-slicker-i231 时将我指向此目录而不是控制器和操作.当我与用户一起删除文件夹时,流程将转到控制器和操作.如何配置 .htaccess 以便 example.com/profile/martin-slicker-i231 将指向控制器和操作,但请求 example.com/profile/martin-slicker-i231/avatar-small.jpg 指向该文件.这是我的 .htaccess

The problem is when i point browser to example.com/profile/martin-slicker-i231 it points me to this directory not the controller and action. When I remove the folder with user then the flow go to the controller and action. How to configure .htaccess so the example.com/profile/martin-slicker-i231 will be pointing to controller and action but request to example.com/profile/martin-slicker-i231/avatar-small.jpg point to the file. Here is my .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]

有人可以帮忙吗?

推荐答案

只需删除说它应该为目录提供服务的部分:

Just remove the portion that says that it should serve directories:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l 
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

现在它将直接提供文件(大小 > 0)和符号链接,但将目录引用和其他 url 发送到您的应用程序

Now it will serve files (with size > 0) and symbolic links directly, but send directory references and other urls to your application

这篇关于htaccess 阻止访问目录但允许访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆