我有适用于 PHP 的 Apache 2.4 Web 服务器,我想使用 .htaccess 使其安全,这样除了根文件夹 (
index.php
) 中的 var/www/html/index.php
之外,没有人能够访问我的文件。问题是,根据我的配置,它仅允许来自 localhost/index.php
而不是 localhost/
的 index.php。我尝试过重定向,但没有太大变化:
DirectoryIndex index.php
# Redirect the root (/) to index.php
RewriteEngine On
RewriteRule ^$ /index.php [L]
# Deny all requests by default
Require all denied
# Allow only access to index.php
<Files "index.php">
Require all granted
</Files>
改变
<Files "index.php">
Require all granted
</Files>
到
<Files ~ "^(index\.php|)$">
Require all granted
</Files>
解决了问题;
<Files ~ "^(index\.php|)$">
:
~
表示正则表达式index.php
或空字符串(在本例中是根目录)