简体   繁体   中英

How to password protect index.php and /, but allow access to all other files and subdirectories.

My question is very similar to this one: getting "authentication required" when requesting / instead of /index.php

However, in the question above, the goal is to allow access to only the root URL or index.php and to password protect access to all other files and subdirectories.

An excellent answer was given:

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

My question is the reverse: I only want to password protect the root URL of the directory or the index.php file, and to allow free access to all other files in the directory. When I used <FilesMatch "index.php"> I succeeded in protecting the index file, but the root url allowed access.

I'm sure the answer is basically obvious to those of you who are more familiar with htaccess than I am. How do I password protect only index.php and the root url? Thank you.

Use it like this:

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
  • Regex pattern ^/(index\\.php)?$ will match / or /index.php and it sets env variable PROTECT
  • Allow from All allows all the URIs
  • Deny from env=PROTECT denies env variable PROTECT

不确定这是不是你真正想要的,但是选项-Indexes怎么样呢?

<Directory /www/somefolder> Options -Indexes FollowSymLinks AllowOverride None </Directory>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM