通过 URL 保护对文件和文件夹的访问



在我的.htaccess文件中,我使用以下文件来防止直接访问文件夹:

Options -Indexes

我正在使用以下内容来阻止访问我们的关键文件:

<Files admin.php>
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    Allow from (my ipaddress)
</Files>

因此,现在用户无法转到 www.domain.com/scripts,因为它会抛出404错误,也无法访问admin.php

但是,如何防止直接访问所有内容?

例如,

如果有人知道文件名,他们仍然可以访问它,例如:www.domain.com/scripts/process.php

怎么办?

使用 mod_rewrite:

将此规则置于所有其他规则之上:

RewriteRule ^scripts(/.*|)$ - [F,NC]

不使用mod_rewrite:

将此代码放入scripts/.htaccess

Order Deny,Allow
Deny from all

更新:要阻止直接访问所有文件,请执行以下操作:

# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} ..+[s?]
# return forbidden error if not static files
RewriteRule (?!^.+.(?:jpe?g|gif|bmp|png|tiff|css|js)$)^.*$ - [F]

最新更新