Knowledge Base

How to Set Up Password Protection Using .htaccess

This article will guide you through setting up password protection for directories using the .htaccess file.


1. Create the .htpasswd File:

  • Use a text editor to create a new file named .htpasswd.
  • Generate a username and password combination. You can use online tools to create an encrypted password.
  • Add the username and encrypted password to the .htpasswd file in the format: username:encrypted_password.
  • Save the .htpasswd file.

2. Upload the .htpasswd File:

  • For security reasons, place the .htpasswd file outside of your web root directory to prevent unauthorized access.

3. Create the .htaccess File:

  • Navigate to the directory you want to protect.
  • Create a new file named .htaccess in that directory.
  • Add the following lines to the .htaccess file:
     AuthType Basic
     AuthName "Restricted Area"
     AuthUserFile /path/to/.htpasswd
     Require valid-user
  • Replace /path/to/.htpasswd with the actual path to your .htpasswd file.

4. Save and Upload the .htaccess File:

  • Save the .htaccess file and upload it to the directory you wish to protect.

5. Test the Password Protection:

  • Navigate to the protected directory in your web browser. You should be prompted to enter the username and password you set up.

Important Notes:

  • Ensure that the .htpasswd file is stored in a secure location outside the web root to prevent unauthorized access.
  • Additionally, verify that your server allows the use of .htaccess files and that the AllowOverride directive is enabled in the server configuration.
Please rate this article to help us improve our Knowledge Base.

0 0