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

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

问题描述

我有这种情况。
我正在Zend Framework和htaccess中开发应用程序,将每个请求都指向
index.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...

现在我有这样的链接:

Now I have for example such link:

example.com/profile/martin-slicker-i231

使用Zend路由器将其指向控制器帐户和操作视图配置文件。我想为我的用户添加一些avatart,并在公共文件夹中创建目录(这样,服务器和css和js这样的服务器都可以访问图像)。该目录名为配置文件,其中的子目录为 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天全站免登陆