如何在 PHP 中防止目录访问和显示禁止的错误


how to prevent directory access and show forbidden error in php

  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为什么以及如何修复它?

我用过

# disable directory browsing
Options -Indexes

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

# enable directory browsing
Options +Indexes

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

# prevent folder listing
IndexIgnore *

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

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

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

它还包含许多其他有用的命令,用于.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

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

如果要

拒绝访问所有文件:

deny from all

如果要禁用目录列表:

IndexIgnore *
相关文章: