Apache htaccess:阻止直接访问文件夹中的index.html

时间:2013-11-28 01:27:12

标签: html apache .htaccess

我的.htaccess

中有以下代码
<IfModule mod_rewrite.c>
    RewriteEngine On

    # block text, html and php files in the folder from being accessed directly
    RewriteRule ^content/(.*)\.(txt|html|php)$ error [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule . index.php [L]
</IfModule>

# Prevent file browsing
Options -Indexes

阻止访问内容文件夹中的txt,html,php文件。它工作得很好,如果访问以下URI,它会阻止index.html

mysite.com/content/index.html

但如果从URI中省略了它,则它不会阻止显示的index.html内容:

mysite.com/content/

如何解决这个问题?谢谢。

2 个答案:

答案 0 :(得分:7)

您需要禁用目录索引,而不是阻止任何内容。只需将其添加到.htaccess文件中:

DirectoryIndex none.none
Options -Indexes

第一行是告诉apache在用户导航到该文件夹​​时不要提供“index.html”。有关详情,请访问DirectoryIndex doc。

其次是告诉apache不要显示文件夹的内容,因为没有要显示的默认文件(第一行禁用它)

答案 1 :(得分:0)

如果在主目录.htaccess文件中添加 DirectoryIndex none.none ,您将禁用主索引,这可能是不合需要的。

你的修复是:

RewriteRule ^content/?$ error [R=301,L]
RewriteRule ^content/(.*)\.(txt|html|php)$ error [R=301,L]

以上一行告诉Apache将所有请求重定向到内容内容/ 的错误。

相关问题