How to password protect directory in Apache

August 3, 2020 ≈ 24 seconds

Sometimes, you just want to set quick and simple password protection for a folder. That's how it can be done in Apache.

Create .htpasswd file in folder, you want to protect, using oneliner:

htpasswd -b -c .htpasswd user password

Write these lines to the folder .htaccess file

AuthType Basic
AuthName "protected folder"
AuthUserFile /absolute/path/to/.htpasswd
require valid-user

Can I just use the relative path? —Unfortunately no. If a path is not absolute, it is treated as relative to the ServerRoot.

Subscribe to our newsletter

If you provide url of your website, we send you free design concept of one element (by our choice)

Subscribing to our newsletter, you comply with subscription terms and Privacy Policy