‹ Articles

How to Password Protect a Single File

You can give limited access to a single file on your server by using a .htaccess file, similar to protecting a directory.

Step One: Edit .htaccess

If you haven't already done so, first, open your text editor and create a file named .htaccess.

Add the following lines into the .htaccess file where MYFILE.html is the name of the file you want to protect and APPNAME is the name of your app.

<Files MYFILE.html>
AuthType Basic
AuthName "Authentication Required"
AuthUserFile /srv/users/SYSUSER/apps/APPNAME/public/.htpasswd
Require valid-user

Step Two: Create .htpasswd

If you have already created a .htpasswd file, skip to the next step.

Next, SSH in to your server as the the system user the app belongs to. Run the following command to create your .htpasswd file, replacing APPNAME with the name of your app.)

touch ~/apps/APPNAME/public/.htpasswd

Step Three: Assign Username and Password

Now, use the htpasswd-sp command to add or change a password for a user yourname. (You can substitute any username for yourname).

htpasswd-sp ~/apps/APPNAME/public/.htpasswd yourname

Follow the prompts in the command line to add the new password.

That's It!

When you visit the file through your browser, you'll now be prompted for a username and password.

Still Have Questions?

Don't hesitate to contact us if you can't find the answers to your questions.