简体   繁体   English

如何在 htaccess 中限制其他用户访问文件夹或文件?

[英]How can I restrict access to a folder or a file to other users in htaccess?

I want to restrict access to some folders and files but only when a user tries to access to it through the url, not when the website access to these files.我想限制对某些文件夹和文件的访问,但仅当用户尝试通过 url 访问它时,而不是在网站访问这些文件时。 For example restrict the folders images, javascript,... I tried in different ways but I always got error 500. Basically, I don't want external users to list my website directory and open their files, if it is possible to accomplish.例如限制文件夹图像,javascript,...我尝试了不同的方式,但我总是得到错误 500。基本上,我不希望外部用户列出我的网站目录并打开他们的文件,如果可能的话。

Thanks in advance提前致谢

This is pure mod_rewrite based solution:这是纯基于 mod_rewrite 的解决方案:

RewriteRule ^(includes/|submit\.php) - [F,L,NC]

This will show forbidden error to use if URI contains certain paths.如果 URI 包含某些路径,这将显示禁止使用的错误。

You are getting a 500 error because the container cannot be used in an htaccess file (which is essentially all inside a directory container for the directory that it's in).您收到 500 错误,因为该容器不能在 htaccess 文件中使用(它基本上都在它所在目录的目录容器内)。 What you need to do is remove the container from your htaccess file, and leave the Deny from all bit:您需要做的是从您的 htaccess 文件中删除容器,并从所有位保留拒绝:

htaccess file in your document root:文档根目录中的 htaccess 文件:

Refuse direct access to all files
Order deny,allow
Deny from all
Allow from 127.0.0.1

Then create an htaccess file in the uploads/files/ , uploads/images/pages/ and uploads/images/store/ (and whatever other directories that you want to allow access to):然后在uploads/files/uploads/images/pages/和 uploads/images/store/ (以及您希望允许访问的任何其他目录)中创建一个 htaccess 文件:

Allow from all

put .htaccess and edit with "Deny from all".放入 .htaccess 并使用“拒绝所有人”进行编辑。 That's it.就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM