How to protect directories using Apache passwords in .htaccess?
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=dkp9qUXZ
BY antony.glen
5 years ago
Technical-Solution

Let's check how to add password protection to directories, there are many ways to add password protection to a directory. Here lets see how doing this with .htaccess. It is the built-in feature in Apache using htaccess method. By doing this we could restrict access to certain users or search engines.

For creating an Apache password protected directories we will be needing a password file and the directory name which is to be password protected. We could perform this task by following a 3 step process.

  1. Configure Apache to use .htaccess file.
  2. Make a password file.
  3. Testing our configuration.

1. Configure Apache to use .htaccess file.

Add AllowOverride AuthConfig directive to http.conf file for these directives to work. So our http.conf looks like,


  <Directory /var/www>
  Options Indexes Includes FollowSymLinks MultiViews
  AllowOverride AuthConfig
  Order allow,deny
  Allow from all
  </Directory>

Save and restart Apache server.

2. Make a password file.

Use htpasswd to create password for authentication when using Apache. For example, htpasswd -c password-file username

-c: attribute denotes create new file, will re-write if password file already exists.

username: second one username is used create user for password file.

Password file should be kept were it is not accessible by web.

To add new user using htpasswd, $> htpasswd -c /home/secure/<file> <username>
Make sure the file created is readable by Apache.

Now its time to create our htaccess file. Create an empty file .htaccess in our desired directory. Open the file for editing and add the commands below.

AuthType Basic
  AuthName "Restricted Access"
  AuthUserFile /home/secure/<file>
  Require user <username>

3. Testing our configuration.

Completing the step 2, we have almost finished. Now it is time to test the configuration, open the browser and try to access the directory were htaccess is added, eg:
Go to: http://localhost/project/<dir> or http://example.com/<dir>
If the above procedures are done correctly, the URL will be prompted to one login section requesting username and password.

Hope you have found this article useful, feel free to share your thoughts.

You may also look into other solved configurations related to htaccess over here.

Please fell free to share your thoughts and doubts regarding this here.


RELATED ARTICLE

/themes/custom/zyxpro_light/images/placeholder.png
close

on 04th April 2007 / by webmaster
Linux is the epitome of the FLOSS model. Technical Solution Linux installation Dewey J. Corl (not verified) access_time 02 Aug 2021 - 02:20 Linux is not ready for the general public! (I am a Ubuntu user). For proof, 1. try to simply print labels on a Dymo label printer. It takes a LOT of work to get to the ease of label printing already available in Windows and Mac. 2. try syncing a modern Palm based PDA. Yes, jpilot and others will sync the main data, but pictures and midi files do not get transferred without a lot of extra setup. These are examples of applications that are not ready for the general public. Since an operating system only supports applications, Linux is not ready to be a common desktop for the general public. While we are waiting for the applications to catch up, keep up the good work!! Anoop John (not verified) access_time 02 Aug 2021 - 02:20 Dewey Yes you may be right. But these are not applications that a common man would use. A common man would use one of these applications - Internet browser, Chat Client, Email Client, Document Writer, Presentation Creator, Spreadsheet Editor, Movie Viewer, Music Player, CD/DVD Writer. Both examples you cited are more specific applications that only a small percentage of the whole population uses. It will take time before those hardware vendors identify the need from their perspective to address the Linux community. Krishnadas (not verified) access_time 02 Aug 2021 - 02:20 Dear Mr Anoop, Thank you very much for the free installation of LINUX done in my PC. I am getting immersed in the LINUX and seeing the versatility. To my surprise, I am able to read one of my old backup CD(wherein lot of my valuable file exist) done in DIRECT CD wizard (a custom cd writing software of Easy Cd creator in WIN98) which could not be read in XP. Installation of old version of easycd creator/direct cd program was not possible in XP. I thought that possible i lost all data. Very pleasant start! Srikanth N. S. (not verified) access_time 02 Aug 2021 - 02:20 Hi Anoop, My friend is thrilled with Ubuntu. One of his grouses with windows was that after OS installation, everything else is to be installed separately which is a real headache. WHereas if u install Linux, everything is installed as a package and he is thrilled to bits! Let linux installation spread in Trivandrum and your service in this direction is highly appreciated. Kepp up the good work Regards. PDA Freak (not verified) access_time 02 Aug 2021 - 02:20 Is it possible to install linux on a packardbell notebook ?