To create password protection using `.htaccess` on an Apache web server, you can follow these steps:
1. Create a Password File: First, you need to create a file to store usernames and passwords. You can do this using the `htpasswd` utility. Run the following command in your terminal, replacing `username` with the desired username:
htpasswd -c /path/to/.htpasswd username
You will be prompted to enter and confirm the password for the user. If you’re adding additional users later, omit the `-c` flag.
2. Create or Modify the `.htaccess` File: In the directory you want to protect, create or edit the `.htaccess` file. Add the following lines:
AuthType Basic AuthName "Restricted Area" AuthUserFile /path/to/.htpasswd Require valid-user
Replace `/path/to/.htpasswd` with the actual path to the `.htpasswd` file you created.
3. Adjust Directory Configuration: If you’re applying this to a specific directory, make sure that directory is configured to allow `.htaccess` overrides. This can be done by setting `AllowOverride` to `All` in your Apache configuration file (`httpd.conf` or similar) for the directory where your `.htaccess` file resides.
4. Restart Apache: After making these changes, restart your Apache server to apply the new configurations.