How to Configure Your Website Using htaccess in Linux with Apache Step by Step

 

.htaccess files (or “distributed configuration files”) provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.

 

Step by Step

 

For this to work successfully you will have to be logged in as root or using one of the sudo or su options.

 

Step 1:

 

We will need to create a folder that will have to be authenticated. Since the default location in Apache is /var/www/html we will create it here. You will do this by using the mkdir command.

 

Step 2:

 

Next, we need to add the .htaccess & .htpasswd files to the personal folder. We first need to change the directory of the folder we wish to protect.

 

Step 3:

 

Next, we can create the .htaccess file.

 

Step 4:

 

Press i to insert and add the following content.

 

Step 5:

 

Change “test folder” to the name of your folder and change “USER_NAME” to the user name you wish to use.

 

Step 6:

 

Press your esc button then: wq to save your file in your vi editor.

 

Step 7:

 

Next, we’ll create the .htpasswd  file. We want to run htpasswd on the path of the folder we want to protect.

 

Step 8:

 

You should see something like this:

 

Step 9:

 

Verify that this file was created in your test folder directory. To do this use “ls -a” in your command prompt.

 

Step 10:

 

Next, we will have to edit the Apache httpd.conf (on some systems called the apache2.conf) file.

 

Step 11:

 

You will have to scroll all the way to the bottom to add the following directory.

 

Step 12:

 

Finally, save httpd.conf by typing esc: qw! and restart apache.