我的问题与这个非常相似: getting "authentication required" when requesting / instead of /index.php
但是,在上面的问题中,目标是只允许访问根 URL 或 index.php,并使用密码保护对所有其他文件和子目录的访问。
给出了一个很好的答案:
SetEnvIf Request_URI "^/$" allow=yes
SetEnvIf Request_URI "^/index.php$" allow=yes
AuthType Basic
AuthName "Password Required"
AuthUserFile /var/www/webinterface/.htpasswd
Options +FollowSymLinks
Order Deny,Allow
Satisfy any
Deny from All
Require valid-user
Allow from env=allow
我的问题是相反的:我只想用密码保护目录的根 URL 或 index.php 文件,并允许自由访问目录中的所有其他文件。当我使用 <FilesMatch "index.php">
我成功保护了索引文件,但根 url 允许访问。
我敢肯定,对于那些比我更熟悉 htaccess 的人来说,答案基本上是显而易见的。如何只用密码保护 index.php 和根 url?谢谢你。
最佳答案
像这样使用它:
SetEnvIfNoCase Request_URI "^/(index\.php)?$" PROTECT
AuthType Basic
AuthName "Password Required"
AuthUserFile /var/www/webinterface/.htpasswd
Require valid-user
Satisfy any
Order Allow,Deny
Allow from All
Deny from env=PROTECT
- 正则表达式
^/(index\.php)?$
将匹配/
或/index.php
并设置环境变量保护
Allow from All
允许所有 URIDeny from env=PROTECT
拒绝环境变量PROTECT
关于php - 如何密码保护 index.php 和/,但允许访问所有其他文件和子目录。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46775268/