{"id":583,"date":"2011-07-19T17:17:44","date_gmt":"2011-07-20T00:17:44","guid":{"rendered":"http:\/\/hybridclassroom.com\/blog\/?p=583"},"modified":"2011-09-08T19:40:11","modified_gmt":"2011-09-09T02:40:11","slug":"how-to-password-protecting-a-website-directory-with-htaccess","status":"publish","type":"post","link":"https:\/\/www.hybridclassroom.com\/blog\/?p=583","title":{"rendered":"How-To: Password Protecting a Website Directory with .htaccess"},"content":{"rendered":"<p>How-To: Password Protecting a Website Directory with .htaccess<\/p>\n<p>by Richard White<\/p>\n<p>2011-07-19<\/p>\n<p>A friend of mine who maintains a website for his classes recently asked me how to go about creating a password-protected folder for the site. He wants to store materials on there that would be accessible to his students who would use a Name and Password to browse the folder.<\/p>\n<p>In other words, when a user tries to go to a certain location on this teacher&#8217;s website, he wants them to have to authenticate with a Name and Password before they&#8217;ll be allowed to enter the site.<\/p>\n<p><a href=\"http:\/\/hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-4.41.00-PM.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-4.41.00-PM.jpg\" alt=\"\" title=\"Screen shot 2011-07-19 at 4.41.00 PM\" width=\"412\" height=\"191\" class=\"aligncenter size-full wp-image-609\" srcset=\"https:\/\/www.hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-4.41.00-PM.jpg 412w, https:\/\/www.hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-4.41.00-PM-300x139.jpg 300w\" sizes=\"(max-width: 412px) 100vw, 412px\" \/><\/a><\/p>\n<p>Here&#8217;s how you password protect a folder, in three easy steps.<\/p>\n<h3>1. Create the folder that will store the protected material.<\/h3>\n<p>For our example, we&#8217;ll assume that my account on the webserver is called <i>rwhite<\/i>, and the website files are all stored in <i>public_html<\/i>. In that directory <i>public_html<\/i>, create a new directory &#8220;secretstuff&#8221;, which is where we&#8217;ll be storing our password-protected materials. This folder should have a permission of 755.<\/p>\n<p>(There are lots of different ways to &#8220;create a folder&#8221;, depending on how you manipulate files on your server. You might <i>ssh<\/i> in to the server, you might use Dreamweaver or Coda, &#8230; If you&#8217;re not sure how to manipulate files and directories on your server, learn how to do that first and then come back here!)<\/p>\n<p>So in terms of your websites directory structure, here&#8217;s what we have so far (your files and directories will look different from mine&#8211;the ones shown are for example only):<\/p>\n<pre style=\"background: #000; color: #0f0; font-size: 150%; padding: 10px;\">\r\n\/\r\n|---home\/\r\n    |---rwhite\/\r\n\t|---logs\/\r\n        |---mail\/\r\n        |---public_ftp\/\r\n        |---public_html\/\r\n\t    |---about.html\r\n            |---index.html\r\n            |---secretstuff\/\r\n<\/pre>\n<h3>2. Use a text editor to create a text file called &#8220;.htaccess&#8221; in the folder that you want to protect.<\/h3>\n<p>To keep unauthorized users from peeking inside the directory <i>secretstuff<\/i>, you&#8217;ll need to add two additional files to your website that will instruct the server under what conditions it should display the contents.<\/p>\n<p>The first file is a text file called <i>.htaccess<\/i> that is stored in the <i>secretstuff<\/i> directory. Note that this filename doesn&#8217;t really have a name&mdash;it only has an extension (the eight letters after the period). That means that this file won&#8217;t show up in most directory listings unless you specifically tell your computer to list ALL files.<\/p>\n<p>Use a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Text_editor\">text editor<\/a>&mdash;Notepad, TextWrangler, BBEdit, TextMate, vi, emacs, nano, edit, whatever&mdash;to create the <i>.htaccess<\/i> file in the <i>secretstuff<\/i> directory. The file <i>.htaccess<\/i> should include these four lines:<\/p>\n<pre style=\"background: #000; color: #0f0; font-size: 150%; padding: 10px; \">\r\nAuthName \"Secret Stuff\"\r\nAuthType Basic\r\nAuthUserFile \"\/home\/rwhite\/.htpasswds\/public_html\/secretstuff\/passwd\"\r\nRequire valid-user\r\n<\/pre>\n<p>Be sure to save the file with the name <i>.htaccess<\/i> (including the period in front!). That file should have permissions 644.<\/p>\n<p>What does all of that mean?<\/p>\n<p>First of all, <i>.htaccess<\/i> is used by the Apache web server to do all sorts of things on your website, and you probably already have a few .htaccess files sprinkled here and there on your site&mdash;we don&#8217;t want to mess with those. This particular .htaccess file in the secretstuff directory is simply being used to control access to that directory.<br \/>\nThe four lines in that file, in order, say:<\/p>\n<ol>\n<li>Display this name in the authentication dialog box.<\/li>\n<li>Use Basic http authentication.<\/li>\n<li>Find the file containing passwords at this location on the server (see step 3 below).<\/li>\n<li>Make sure user has been authenticated before giving them access to this folder.<\/li>\n<\/ol>\n<p>The only really tricky part about this step is the location of the password file. Note that the <i>.htpasswds<\/i> directory listed here is NOT contained in \/home\/rwhite\/public_html&mdash;placing that directory in a publicly-accessible folder is a security risk. Instead, the <i>.htpasswds<\/i> directory is contained in <i>\/home\/rwhite<\/i>, which is not accessible by a browser. That location IS accessible to Apache, however, which will look at that location to find out which users will provide what passwords in order to gain access to the protected folder.<\/p>\n<p>So here&#8217;s what we&#8217;ve got now:<\/p>\n<pre style=\"background: #000; color: #0f0; font-size: 150%; padding: 10px;\">\r\n\/\r\n|---home\/\r\n    |---rwhite\/\r\n\t|---logs\/\r\n        |---mail\/\r\n        |---public_ftp\/\r\n        |---public_html\/\r\n\t    |---about.html\r\n            |---index.html\r\n            |---secretstuff\/\r\n\t\t|---.htaccess\r\n<\/pre>\n<h3>3. Use a text editor to create the text file called &#8220;passwd&#8221; that we&#8217;ll place in the .htpasswds directory.<\/h3>\n<p>As discussed above, the <i>passwd<\/i> file will be be located someplace where a browser can&#8217;t get to it. Here&#8217;s where we&#8217;re going to put it.<\/p>\n<pre style=\"background: #000; color: #0f0; font-size: 150%; padding: 10px;\">\r\n\/\r\n|---home\/\r\n    |---rwhite\/\r\n\t|---.htpasswds\r\n\t    |---public_html\r\n              \t|----secretstuff\r\n\t\t    |---passwd\r\n        |---logs\/\r\n        |---mail\/\r\n        |---publc_ftp\/\r\n\t|---public_html\/\r\n            |---about.html\r\n            |---index.html\r\n\t    |---secretstuff\/\r\n\t\t|---.htaccess\r\n<\/pre>\n<p>Note that if you don&#8217;t already have a directory called <i>.htpasswds<\/i>, you&#8217;ll need to create it, and then nest inside it the <i>public_html<\/i> and <i>secretstuff<\/i> directories. (Although some tutorials will instruct you to place a single .htpasswd file in those location, creating a directory will give you more flexibility later on, should you choose to create additional .htaccess authentications.) The <i>.htpasswds<\/i> directory and those nested inside it should all have permissions of 644, as should the <i>passwd<\/i> file itself.<\/p>\n<p>Now, what actually goes IN the <i>passwd<\/i> file? For our purposes, it&#8217;s going to consist of a single line: the Name, a colon, and then the Password that a user will need to get into the password-protected <i>secretstuff<\/i> directory.<\/p>\n<p>Assuming we want to allow <i>ImaStudent<\/i> to access the directory using a password of <i>123456<\/i> (not a very good password, obviously), that one line in the file <i>passwd<\/i> will contain both of those pieces of information, and look like this:<\/p>\n<pre style=\"background: #000; color: #0f0; font-size: 150%; padding: 10px;\">\r\nImaStudent:EdQXJLHVRhCFo\r\n<\/pre>\n<p>Whoa, whoa, whoa. Where did <i>EdQXJLHVRhCFo<\/i> come from? That doesn&#8217;t look like our password <i>123456<\/i> at all.<\/p>\n<p>That&#8217;s because EdQXJLHVRhCFo is an htpasswd &#8220;hash&#8221; of 123456. Apache is so security-conscious that it doesn&#8217;t even want to know what your real password is&mdash;it only wants to store a &#8220;hash&#8221;, or one-way coded version, of that password. When a user enters their password into the authentication box, that hashed password (the EdQXJLHVRhCFo, converted from 123456) will be compared to the hashed version in your <i>passwd<\/i> file.<\/p>\n<p>So how do you know what hash to include in your <i>passwd<\/i> file?<\/p>\n<p>There are a few ways to do this. One way is to go to a website like <a href=\"http:\/\/tools.dynamicdrive.com\/password\/\">this one<\/a> and enter your password into the indicated field.<\/p>\n<p>Or, if you wish to do it yourself on the computer, open up a Terminal and on the command line, type<\/p>\n<pre style=\"background: #000; color: #0f0; font-size: 150%; padding: 10px; \">\r\n$ htpasswd -ndb ImaStudent 123456\r\n<\/pre>\n<p>&#8230;and you&#8217;ll get a line that you can paste into your <i>passwds<\/i> file.<\/p>\n<p>Once you&#8217;ve got everything put together, try going to the page that you&#8217;ve created and see if you can see anything. If all works as planned, you&#8217;ll be confronted by a dialog box that looks something like this.<\/p>\n<p><a href=\"http:\/\/hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-4.41.00-PM.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-4.41.00-PM.jpg\" alt=\"\" title=\"Screen shot 2011-07-19 at 4.41.00 PM\" width=\"412\" height=\"191\" class=\"aligncenter size-full wp-image-609\" srcset=\"https:\/\/www.hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-4.41.00-PM.jpg 412w, https:\/\/www.hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-4.41.00-PM-300x139.jpg 300w\" sizes=\"(max-width: 412px) 100vw, 412px\" \/><\/a><\/p>\n<p>By typing in the appropriate Name and Password, the browser should proceed to display the previously hidden contents of that folder!<\/p>\n<p>There&#8217;s one more thing you may need to take care of, however, depending on how you want to use the new <i>secretstuff<\/i> folder. If you&#8217;re going to use it to serve up regular webpages like <i>index.html<\/i> then you&#8217;re all done: once a user&#8217;s attempt to access the directory is authenticated, those pages will appear just as in any other directory.<\/p>\n<p><a href=\"http:\/\/hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-5.04.14-PM.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-5.04.14-PM.jpg\" alt=\"\" title=\"Screen shot 2011-07-19 at 5.04.14 PM\" width=\"559\" height=\"241\" class=\"aligncenter size-full wp-image-611\" srcset=\"https:\/\/www.hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-5.04.14-PM.jpg 559w, https:\/\/www.hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-5.04.14-PM-300x129.jpg 300w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><\/a><\/p>\n<p>You may, however, wish to just dump a bunch of files into that folder that students can access. They might be text files, or Word documents, or graphics. If this is the case, it may also be that your webserver doesn&#8217;t by default allow these files to be &#8220;indexed,&#8221; or listed, and you&#8217;ll get an error message like the one above.<\/p>\n<p>This is easily fixed. In your <i>.htaccess<\/i> file&mdash;the same one that we were working on in step 2 above&mdash;add a fifth line:<\/p>\n<pre style=\"background: #000; color: #0f0; font-size: 150%; padding: 10px; \">\r\nAuthName \"Secret Stuff\"\r\nAuthType Basic\r\nAuthUserFile \"\/home\/rwhite\/.htpasswds\/public_html\/secretstuff\/passwd\"\r\nRequire valid-user\r\nOptions +Indexes\r\n<\/pre>\n<p>This line will allow this directory&#8217;s contents to be Indexed, or listed, even if there aren&#8217;t any <i>html<\/i> files to be displayed.<br \/>\n<a href=\"http:\/\/hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-5.15.29-PM.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-5.15.29-PM.jpg\" alt=\"\" title=\"Screen shot 2011-07-19 at 5.15.29 PM\" width=\"668\" height=\"318\" class=\"aligncenter size-full wp-image-612\" srcset=\"https:\/\/www.hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-5.15.29-PM.jpg 668w, https:\/\/www.hybridclassroom.com\/blog\/wp-content\/uploads\/2011\/07\/Screen-shot-2011-07-19-at-5.15.29-PM-300x142.jpg 300w\" sizes=\"(max-width: 668px) 100vw, 668px\" \/><\/a><\/p>\n<p>Now students are able to view or download those files simply by clicking on them in the browser window.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How-To: Password Protecting a Website Directory with .htaccess by Richard White 2011-07-19 A friend of mine who maintains a website for his classes recently asked me how to go about creating a password-protected folder for the site. He wants to store materials on there that would be accessible to his students who would use a &hellip; <a href=\"https:\/\/www.hybridclassroom.com\/blog\/?p=583\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How-To: Password Protecting a Website Directory with .htaccess<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[70,75,79],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/583"}],"collection":[{"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=583"}],"version-history":[{"count":40,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/583\/revisions"}],"predecessor-version":[{"id":684,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/583\/revisions\/684"}],"wp:attachment":[{"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}