php 如何防止目录访问并在php中显示禁止错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10773025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:00:48  来源:igfitidea点击:

how to prevent directory access and show forbidden error in php

phpapache

提问by user_quest

  1. I am new to .htaccess, i want to know how to use .htacces to prevent direct access via url of my folder e.g. localhost/mycart/images/ OR localhost/mycart/css should show forbidden message.

  2. currently i've put index.php in every folder of my website and echo the messages is it good way to do it.

  3. when i access my website like www.mycart/index.php/css or www.mycart/index.php/images it show website without images and css why and how to fix it?

  1. 我是 .htaccess 的新手,我想知道如何使用 .htacces 来防止通过我的文件夹的 url 直接访问,例如 localhost/mycart/images/ 或 localhost/mycart/css 应该显示禁止消息。

  2. 目前我已将 index.php 放在我网站的每个文件夹中,并回显消息是否是这样做的好方法。

  3. 当我访问我的网站时,如 www.mycart/index.php/css 或 www.mycart/index.php/images 它显示没有图像和 css 的网站为什么以及如何修复它?

回答by Asif

I have used

我用过了

# disable directory browsing
Options -Indexes

Conversely, to enable directory browsing, use the following directive:

相反,要启用目录浏览,请使用以下指令:

# enable directory browsing
Options +Indexes

Likewise, this rule will prevent the server from listing directory contents:

同样,此规则将阻止服务器列出目录内容:

# prevent folder listing
IndexIgnore *

And, finally, the IndexIgnore directive may be used to prevent the display of select file types:

最后,IndexIgnore 指令可用于防止显示选择的文件类型:

# prevent display of select file types
IndexIgnore *.wmv *.mp4 *.avi *.etc

Courtesy From: Prevent Unauthorized Directory Browsingfrom Stupid HTACCESS Tricks.

礼貌来自: 防止未经授权的目录浏览来自愚蠢的 HTACCESS 技巧

It also contains many other useful commands to be used in .htaccessfor security and performance enhancement.

它还包含许多其他用于.htaccess增强安全性和性能的有用命令。

回答by Team Webgalli

You can simply add the following to your .htaccess

您可以简单地将以下内容添加到您的 .htaccess

# Don't listing directory
Options -Indexes
ErrorDocument 403 http://www.your-website-home-page.com/
# Follow symbolic links
Options +FollowSymLinks

# Default handler
DirectoryIndex index.php

This will load only the index.php file while file browsing. If no index.php file is found 403 error happens (Access restricted) and it will send the user to home page

这将在文件浏览时仅加载 index.php 文件。如果没有找到 index.php 文件,则发生 403 错误(访问受限)并将用户发送到主页

回答by Jeroen

If you want to deny access to all files:

如果您想拒绝访问所有文件:

deny from all

If you want to disable directory listing:

如果要禁用目录列表:

IndexIgnore *