Search code examples
.htaccess.htpasswd

How do I protect index.php with .htaccess


I am trying to protect an index.php page of a subfolder with .htaccess.

The protection works as expected when I navigate to example.com/subfolder/index.php. However, when I just go to example.com/subfolder I get a 401 error.

Here is the relevant .htacess code:

AuthUserFile /home/myfolder/subfolder/.htpasswd

AuthName "Please enter your password"

AuthType Basic

<files index.php>
  require valid-user
</files>

What do I need to do differently?


Solution

  • You should put a new .htaccess file in the subfolder directory with just the following in it:

    AuthUserFile /home/myfolder/subfolder/.htpasswd
    AuthName "Please enter your password"
    AuthType Basic
    
    require valid-user
    

    That will password protect the entire directory