htaccess block access to directory but allow acces

2019-03-30 12:00发布

问题:

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

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

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]

Can somebody help?

回答1:

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]

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



回答2:

According to the Apache docs the following will turn off directory listings

<Directory /path/to/directory>
   Options -Indexes
</Directory>

You might also want to look at the documentation for mod_autoindex