How do I password protect a directory?
You can protect any folder (directory) on your website with a username and password. When someone visits that folder, their browser asks them for the username and password before showing anything. This is useful for keeping private files or parts of your website away from the public.
Using Password Protection in StackCP
- Log into StackCP by following this guide.
- In the Security section, select Password Protection.
- In the Select Directory field, enter the folder you want to protect. For example, to protect a folder called
private, enter private. - Enter a Username and a strong Password.
- Select Add Password Protection.
To remove the protection later, go back to Password Protection in StackCP, find the folder and remove it.
This uses basic HTTP authentication, so anyone with the username and password can view the protected folder. Only share the details with people who need access.
Using .htaccess instead
If you prefer, you can set up the same protection yourself with two files: a .htpasswd file that holds the usernames and passwords, and a .htaccess file in the folder you want to protect. You can create and edit these files with File Manager in StackCP, or upload them by FTP.
- Find the full path to the folder. The .htaccess file needs the full server path to your .htpasswd file. In StackCP, go to the Domains section, where you'll see the full folder path for each domain. Add the name of the folder you want to protect to the end of it. For example, for a folder called
private, the path would be/your/domain/path/private. - Create the .htpasswd file. Each line holds one username and an encrypted password, separated by a colon, for example:
admin:$apr1$jF3s9P1q$Qy7mVnW0cD2yE5rT8uI1o.
The password must be encrypted, not plain text. You can create a line with thehtpasswd -n usernamecommand on a computer that has the Apache tools installed, or with an htpasswd generator tool. Save the file as.htpasswdin the folder you want to protect. - Create the .htaccess file in the same folder, containing the following. Replace the path with the one from step 1:
AuthType Basic
AuthName "Restricted area"
AuthUserFile /full/path/to/private/.htpasswd
Require valid-user
If the folder already has a .htaccess file (for example, a WordPress site), add these lines to the existing file rather than replacing it.