{"id":7,"date":"2008-03-24T12:11:46","date_gmt":"2008-03-24T16:11:46","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/2008\/03\/24\/using-htaccess-file-to-control-web-directory-access\/"},"modified":"2008-03-24T12:11:46","modified_gmt":"2008-03-24T16:11:46","slug":"using-htaccess-file-to-control-web-directory-access","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2008\/03\/24\/using-htaccess-file-to-control-web-directory-access\/","title":{"rendered":"Using .htaccess file to control web directory access"},"content":{"rendered":"<p>Naturally, if the .htaccess (ht.acl in Windows) does not already exist in the directory we wish to protect, we must create it first.  It is a plain text file, so you may use any text editor to create\/modify this file, such as pico, emacs, Notepad, or TextEdit.<\/p>\n<p>Our first step is to add these lines below to the .htaccess file.<\/p>\n<pre class=\"code\">\nAuthName \"This is a restricted area, please log in first.\"\nAuthType Basic\nAuthUserFile \/directory\/path\/passwdfile\n<\/pre>\n<p>AuthName is the text that will appear in the browser pop-up when the user is challenged.  AuthType value of &#8220;Basic&#8221; means we are using basic HTTP authentication.  AuthUserFile is the path and file name of our password file; more on that later.<\/p>\n<p>Also in the .htaccess file, we add a list of user names we wish to allow to access the web directory we are locking down.  For example:<\/p>\n<pre class=\"code\">\nrequire user jdoe\nrequire user spannu\n<\/pre>\n<p>We are now done with the .htaccess file.  Now we just have to create the password file.  In the Apache bin, there is an executable called &#8220;htpasswd&#8221;.  The first example below is used to create a new password file with the user &#8220;jdoe&#8221;; note that when using the -c parameter to create a new file, we will <b>overwrite<\/b> any password file that exists in the same directory, so be careful.  To add a new user to an existing file, we should run the second example, the difference being the lack of the -c parameter.<\/p>\n<pre class=\"code\">\nhtpasswd -c -b \/directory\/path\/passwdfile jdoe secUr3Pwd\n\nhtpasswd -b \/directory\/path\/passwdfile spannu an0therPwd\n<\/pre>\n<p>The -b parameter allows us to type in the password in the command line, which is helpful when you are setting up a script that creates a large number of users at once.  If having the password in the command line cache is a concern, just remove the -b parameter, and we will be prompted to enter a password for each user.<\/p>\n<p>We should now be all set.  The next web visitor that reaches the directory where the .htaccess file resides should be challenged with a password prompt.<\/p>\n<p>To remove a user from a certain password file:<\/p>\n<pre class=\"code\">\nhtpasswd -D \/directory\/path\/passwdfile jdoe\n<\/pre>\n<p>For our reference, below is the help text for the htpasswd command.<\/p>\n<pre class=\"code\">\nUsage:\n        htpasswd [-cmdpsD] passwordfile username\n        htpasswd -b[cmdpsD] passwordfile username password\n\n        htpasswd -n[mdps] username\n        htpasswd -nb[mdps] username password\n -c  Create a new file.\n -n  Don't update file; display results on stdout.\n -m  Force MD5 encryption of the password (default).\n -d  Force CRYPT encryption of the password.\n -p  Do not encrypt the password (plaintext).\n -s  Force SHA encryption of the password.\n -b  Use the password from the command line rather than prompting for it.\n -D  Delete the specified user.\nOn Windows, NetWare and TPF systems the '-m' flag is used by default.\nOn all other systems, the '-p' flag will probably not work.\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The .htaccess file (ht.acl in Windows) is a great with everything from redirection to preventing bandwidth theft.  Here we look at how we can use it to implement password protection for a directory.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-apache"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":0,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}