Configure Your Website Using Htaccess in Linux with Apache

.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.

Steps

  1. For this to work successfully you will have to be logged in as root or using one of the sudo or su options.
  2. We will need to create the 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.
  3. 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.
  4. Next we can create the .htaccess file.
  5. Press i to insert and add the following content.
  6. Change "test folder" to the name of your folder and change "USER_NAME" to the user name you wish to use.
  7. Press your esc button thenĀ :wq to save your file in your vi editor.
  8. Next we'll create the .htpasswd file. We want to run htpasswd on the path of the folder we want to protect.
  9. You should see something like this:
  10. Verify that these file were created in your testfolder directory. To do this use "ls -a" in your command prompt.
  11. Next we will have to edit the apache httpd.conf (on some systems called the apache2.conf) file.
  12. You will have to scroll all the way to the bottom to add the following directory.
  13. Finally save httpd.conf by typing escĀ :qw! and restart apache.

Related Articles

Sources and Citations