如何使用Require指令在Apache 2.4中限制文件访问

5

我有一个目录结构,如下所示,用于运行在Ubuntu 14.04上的Apache 2.4.7网站:

/websites/mywebsite/index.htm
/websites/mywebsite/private.htm
/websites/myqwbsite/folder/privatefile.ext
/websites/mywebsite/folder/subfolder/publicfile.ext

在该网站的Apache配置中,我有以下内容:
<Directory /websites/mywebsite/>
   AllowOverride Limit
   Require all granted
</Directory>

我希望在网站文件夹中使用.htaccess文件,使得private.htm和privatefile.ext文件的访问被拒绝,而其它文件则允许。

我尝试了以下两个.htaccess文件:

/websites/mywebsite/.htaccess:

<FilesMatch (private.*.htm)$>
   Require all denied
</FilesMatch>

/websites/mywebsite/folder/.htaccess:

Require all denied

/websites/mywebsite/folder/subfolder/.htaccess:

Require all granted

然而,Apache在/websites/mywebsite/.htaccess处给出了500错误 - "Require not allowed here"。

我该如何使用与Apache 2.4兼容的配置实现我想要的功能(即,我不想加载mod_access_compat并使用旧的样式配置)?


我还进行了一个测试,只在/websites/mywebsite/folder/subfolder/目录下放置.htaccess文件。所有其他.htaccess文件都被删除了。尽管Apache文档明确表示允许在那里使用"Require",但Apache仍然声称"Require not allowed here"。详情请参考:http://httpd.apache.org/docs/current/mod/mod_authz_core.html#require - dodexahedron
1个回答

7
在网站的Apache配置中,您需要扩展AllowOverride属性。添加:FileInfo和AuthConfig。或者设置"AllowOverride All"。
我遇到了同样的问题,通过以下配置解决:
<Directory /websites/mywebsite/>
   Options +FollowSymLinks +SymLinksIfOwnerMatch
   AllowOverride FileInfo AuthConfig
</Directory>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接