0

I have a .htpasswd file in the root of my domain and a .htaccess file which should protect a single index.php file. Unfortunately it appears asif index.php files in subdirectories are also protected. Is it possible to make this work on a single file and leave files in subdirectories untouched?

This is my current .htaccess file.

<Files "index.php">
AuthName "Users zone"
AuthType Basic
AuthUserFile /home/deb69824/domains/.htpasswd
require valid-user
</Files>
joren
  • 89
  • 1
  • 9

2 Answers2

1

Interesting problem. Using Files or FilesMatch won't help you since that only matches file names irrespective of the directory files reside in. Unfortunately Location directive is not allowed in .htaccess file.

Luckily there is a directive that you can make use of here, i.e. mod_setenvif

# set env variable if URI is /index.php
SetEnvIf Request_URI "^/index\.php$" HOME_INDEX

# use BASIC authentication only when env variable HOME_INDEX is set
AuthType Basic
Authname "Users zone"
AuthUserFile /home/deb69824/domains/.htpasswd

Require valid-user
Satisfy any
Order   allow,deny
Allow from all
Deny from env=HOME_INDEX
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks for your reply. This did not work for me unfortunately but it did lead to a solution. I kept my original .htaccess file and added another one in my subdirectory containing "Satisfy any". This kept the subdirectory out of password protection. The only downside is that I'd have to put the .htaccess file in every subdirectory I would make. Luckily I only need one for the moment. Thanks for you help! – joren Oct 07 '13 at 15:32
  • If this answer helped you solve your problem, please consider marking it as "accepted", so users facing a similar problem in the future will be able to see it easily. – anubhava Oct 07 '13 at 15:59
0

Include a slash, which indicates the exact location of the file, e.g. in the root of the directory?

<Files "./index.php">
AuthName "Users zone"
AuthType Basic
AuthUserFile /home/deb69824/domains/.htpasswd
require valid-user
</Files>
Sidney Gijzen
  • 1,931
  • 3
  • 24
  • 27