{"id":68,"date":"2012-09-16T16:41:00","date_gmt":"2012-09-16T16:41:00","guid":{"rendered":"https:\/\/www.tech-and-dev.com\/blog\/2012\/09\/16\/password-protecting-a-directory-with-htaccess-and-htpasswd\/"},"modified":"2021-02-22T01:01:00","modified_gmt":"2021-02-22T01:01:00","slug":"rotecting-directory-or-subdirectory-with-htaccess-htpasswd","status":"publish","type":"post","link":"https:\/\/www.tech-and-dev.com\/blog\/2012\/09\/rotecting-directory-or-subdirectory-with-htaccess-htpasswd.html","title":{"rendered":"Password protecting a directory with htaccess and htpasswd"},"content":{"rendered":"<h3>Password Protecting a web directory with htaccess<\/h3>\n<div style=\"text-align: justify;\">Password protecting a web directory can help make a directory private and only accessible to one or several persons that have the username(s) and password(s).<\/div>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/htaccess.gif\"><img decoding=\"async\" title=\"Password Protect htaccess\" src=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/htaccess.gif\" alt=\"htaccess\" border=\"0\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<h3>How to password protect a directory?<\/h3>\n<h4><span style=\"color: #3d85c6;\">htaccess <\/span><\/h4>\n<ol style=\"text-align: justify;\">\n<li>Go to the directory you want protected.<\/li>\n<li>Check if you have a <b>.htaccess<\/b> file.<\/li>\n<li>If a htaccess file is available, you have to edit it, otherwise create a new <b>.htaccess<\/b> file.<\/li>\n<li>Add the following to the htaccess file:<\/li>\n<\/ol>\n<div style=\"text-align: justify;\">\n<blockquote>\n<div style=\"background-color: #cccccc; padding: 5px;\"><span style=\"font-family: 'Courier New',Courier,monospace;\">AuthUserFile \/home\/linuxuser\/.htpasswd<\/span><br \/>\n<span style=\"font-family: 'Courier New',Courier,monospace;\">AuthType Basic<\/span><br \/>\n<span style=\"font-family: 'Courier New',Courier,monospace;\">AuthName &#8220;Tech and Dev Example&#8221;<\/span><br \/>\n<span style=\"font-family: 'Courier New',Courier,monospace;\">Require valid-user<\/span><\/div>\n<\/blockquote>\n<blockquote><p><b>AuthUserFile:<\/b> this is the path of the .htpasswd file where the username and password will be stored. The file can be located anywhere, but ideally, it&#8217;s better if it&#8217;s placed in a non public directory where users can&#8217;t access it from the web.<\/p>\n<p><b>AuthType:<\/b> should be <b>Basic<\/b><\/p>\n<p><b>AuthName:<\/b> The title of the window that will be authenticating the user. For example if you&#8217;re password protecting your private files, you might want to change this to &#8220;My Private Files&#8221;.<\/p><\/blockquote>\n<\/div>\n<p>&nbsp;<\/p>\n<h4><span style=\"color: #3d85c6;\">htpasswd<\/span><\/h4>\n<div style=\"text-align: justify;\">This is the file where the username and password are stored.<\/div>\n<div style=\"text-align: justify;\">This file is usually called htpasswd (by convention if you want), however you can rename this file to anything you want. Many applications use their appname followed by htpasswd, for example <i>.awstats-htpasswd<\/i><\/div>\n<div style=\"text-align: justify;\">It&#8217;s always a better idea to keep this file outside of the public folder (www or public_html&#8230;)<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">Assuming you want the username to be <i>username<\/i> and the password to be <i>password<\/i>, write the following command in SSH:<\/div>\n<blockquote>\n<div style=\"background-color: #cccccc; padding: 5px;\"><span style=\"font-family: 'Courier New',Courier,monospace;\">#htpasswd -nb username password<\/span><\/div>\n<\/blockquote>\n<div style=\"text-align: justify;\">Then inside the htpasswd file write the output value from the SSH (the password might be different since everytime a different salt value is used):<\/div>\n<div style=\"text-align: justify;\"><i>username:60lwxfC9Ln84g<\/i><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">You can add as many usernames and passwords as you like, for example:<\/div>\n<div style=\"text-align: justify;\"><i>username:60lwxfC9Ln84g<br \/>\nusername2:30dJVEURUeYJc<\/i><\/div>\n<p>&nbsp;<\/p>\n<h3>How is the password getting encrypted?<\/h3>\n<div style=\"text-align: justify;\">To understand more on how the password is generated and encrypted, you can check my previous post: <a href=\"http:\/\/www.tech-and-dev.com\/2012\/07\/understanding-the-encryption-process-in-htpasswd-file.html\" target=\"_blank\" rel=\"noopener\"> Understanding the encryption process in htpasswd file.<\/a><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">To generate a username\/password, you can check our online <a href=\"http:\/\/lab.tech-and-dev.com\/encryption_tools.php\" target=\"_blank\" rel=\"noopener\">Encryption Tools<\/a>.<\/div>\n<p>&nbsp;<\/p>\n<h3>Example<\/h3>\n<p><a href=\"http:\/\/lab.tech-and-dev.com\/protecteddir\/\" target=\"_blank\" rel=\"noopener\">http:\/\/lab.tech-and-dev.com\/protecteddir\/<\/a><br \/>\nUsername: <i>username<\/i><br \/>\nPassword: <i>password<\/i><br \/>\nor<br \/>\nUsername: <i>username2<\/i><br \/>\nPassword: <i>password2<\/i><\/p>\n<p>Any questions or suggestions? Please leave a comment below!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Password Protecting a web directory with htaccess Password protecting a web directory can help make a directory private and only accessible to one or several persons that have the username(s) and password(s). &nbsp; How to password protect a directory? htaccess Go to the directory you want protected. Check if you have a .htaccess file. If [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[83,84,40,85,6,28,31,7],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-encryption-tools","category-ftp","category-htaccess","category-htpasswd","category-linux","category-security","category-server","category-ssh"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":1,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions\/212"}],"wp:attachment":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}