How to Password Protect a Single File using .htacess File?

Admin   Laravel   140  2021-03-14 18:40:02

Hi Guys,

In this article, I am going to learn you how to password protect a single file on your server by using .htaccess file, similar to protecting a directory. We will talk about if you can limited access to a single file on your server using .htaccess. You can only protect a single file in any directory on your server.

Here, I am tried to password protect a single file using .htaccess. If you want to how to password protect a single file on your server then you can use bellow example. This tutorial will give you not access single file without password and username on your server. In this blog i will give you password protecting a single file on your website. we guide you through password protecting a single file via your htaccess file.

In this article explains how to password protect your directory by creating an .htaccess and .htpasswd file. We will show password protecting your site with an .htaccess file. Here I will give you easiest way to password protect your site. The following steps are covered in this article.

Step 1 : 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 myTest.html is the name of the file you want to protect and APPNAME is the name of your app.

 

<Files myTest.html>

AuthType Basic

AuthName "Authentication Required"

AuthUserFile /srv/users/SYSUSER/apps/APPNAME/public/.htpasswd

Require valid-user

</Files>

Step 2 : Create .htpasswd

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

Now, 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 3 : 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

// OR

htpasswd -c /var/www/html/public/abc/.htpasswd user1

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

I hope It will help you....