Knowledge Base

How to Set Up Password Protection Using .htaccess

Website configuration logo

This article will show you how to set up password protection using .htaccess.


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.

Create 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.

Create the .htaccess 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.

Next: How to Limit Access to Files or Directories Using .htaccess covers restricting access by IP address rather than by password.

Please rate this article to help us improve our Knowledge Base.

0 0