htaccess 阻止访问目录但允许访问文件

编程入门 行业动态 更新时间:2024-10-28 20:21:44
本文介绍了htaccess 阻止访问目录但允许访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有这样的情况.我正在 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/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/profile/martin-slicker-i231 时将我指向此目录而不是控制器和操作.当我与用户一起删除文件夹时,流程将转到控制器和操作.如何配置 .htaccess 以便 example/profile/martin-slicker-i231 将指向控制器和操作,但请求 example/profile/martin-slicker-i231/avatar-small.jpg 指向该文件.这是我的 .htaccess

The problem is when i point browser to example/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/profile/martin-slicker-i231 will be pointing to controller and action but request to example/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-20 00:46:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/972848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   目录   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!