{"id":622,"date":"2012-12-16T18:50:30","date_gmt":"2012-12-16T18:50:30","guid":{"rendered":"http:\/\/techblog.vifashion.vn\/?p=120"},"modified":"2012-12-16T18:50:30","modified_gmt":"2012-12-16T18:50:30","slug":"password-protection-for-you-domain-using-htaccess","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/password-protection-for-you-domain-using-htaccess\/","title":{"rendered":"Password protection for you domain using .htaccess"},"content":{"rendered":"
What You Need<\/strong> You will also need a FTP client if you want to create your .htaccess file on your own system, then upload it to the web server.<\/p>\n The only other thing you need is a web hosting account from Anchor. This would simply be your FTP account information that you received when your service started. To connect via SSH you would just use the same hostname, username, and password as your FTP account<\/p>\n Step By Step Instructions<\/strong> 1. Create a file called .htaccess in directory members that looks like this:<\/p>\n AuthType Basic Notes:<\/p>\n * In the AuthUserFile line, replace USERNAME with your ftp username.<\/p>\n * The .htaccess file must be an ASCII text document. 2. Use the htpasswd command, from your home directory, to create a password file called .htpasswd in your home directory:<\/p>\n SSH to your home directory. This is simply done by connecting with your SSH client and NOT entering any path, and NOT changing directories after connecting.<\/p>\n After connecting to your home directory via SSH, enter:<\/p>\n # htpasswd -c .htpasswd memberone<\/p>\n Type the password — memberonepassword — twice as instructed.<\/p>\n 3. That’s the setup done. Now test by trying to access a file in the directory members; your browser should demand a username and password, and not give you access to the file if you don’t enter memberone and memberonepassword.<\/p>\n Multiple Usernames\/Passwords<\/strong> # htpasswd .htpasswd membertwo Changing Passwords<\/strong> # htpasswd .htpasswd membertwo<\/p>\n Password Protecting Multiple Directories<\/strong> However, if you want to password protect multiple directories, and only allow certain users access to each directory, then you can create a different password file (all in your home directory) for each password protected directory.<\/p>\n Let’s say you have 3 different directories (members, admins, board) you want password protected, and each one has a different set of users that you want to allow access. Then just do the following:<\/p>\n Create three .htaccess files and put them in their appropriate directory:<\/p>\n AuthType Basic AuthType Basic AuthType Basic Remember to replace USERNAME with your ftp username (in lower case).<\/p>\n Create three .htpasswd files in your home directory:<\/p>\n # htpasswd -c .htpasswd.members memberone That’s it. Now when you need to add a user to one of the directories, just issue the htpasswd command on the appropriate .htpasswd file.<\/p>\n Note: There is no correspondence between the usernames and passwords used for any web hosting accounts on your hosting provider’s servers, and usernames and passwords in any specific .htpasswd file. A user does not need to have a hosting account in order to be validated for access to password protected directories. Also, .htaccess protects the entire contents of the directory, not just the web page (HTML file). Any files stored in the directory will also require a password for viewing.<\/p>\n","protected":false},"excerpt":{"rendered":" Make you website private, often used for development enviroment or something like that.<\/p>\n","protected":false},"author":1,"featured_media":121,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57,46],"tags":[63,49,58,64],"aioseo_notices":[],"views":5,"_links":{"self":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/622"}],"collection":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/comments?post=622"}],"version-history":[{"count":0,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/622\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/"}],"wp:attachment":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/media?parent=622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/categories?post=622"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/tags?post=622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\nYou will be executing commands directly on the web server, and the only way to do that is via SSH. SSH is basically a secure form of telnet, and you can use SSH to do anything you might typically do with telnet. So, you must have a SSH client to connect to the web server via SSH.<\/p>\n
\nLet’s suppose you want to restrict files in a directory called members to username memberone with password memberonepassword. Here’s what to do:<\/p>\n
\nAuthName “Restricted access”
\nAuthUserFile \/home\/USERNAME\/.htpasswd
\nrequire valid-user<\/p>\n
\n* A .htaccess file can be created in any word processor but must be saved as text only.
\n* IF you upload your .htaccess file via FTP, the FTP client must be set to ASCII mode for transfer.
\n* For security reasons, the .htaccess file on the server cannot be seen in a directory listing. If you don’t see it after uploading it, don’t worry.
\n* Also note that AuthName can be anything you want. The AuthName field gives the Realm name for which the protection is provided. This name is usually given when a browser prompts for a password, and is also usually used by a browser in correlation with the URL to save the password information you enter so that it can authenticate automatically on the next challenge.<\/p>\n
\nIf you want to give access to a directory to more than one username\/password pair, follow the steps above to create the .htaccess file and to create the .htpasswd file with one user. Then, add additional users to the .htpasswd file by using the htpasswd command without the -c:<\/p>\n
\nNew password:
\nRe-type new password:
\nAdding password for user membertwo<\/p>\n
\nIf you want to change the password for an existing user, simply issue the same command as when you added the user. You will then be prompted for a new password. For example, if the user membertwo already exists and you want to change the password, just SSH to your home directory and enter:<\/p>\n
\nIf you want to password protect multiple directories, and allow all users access to all password protected directories, then all you need to do is put the same .htaccess file in each directory that you want to password protect.<\/p>\n
\nAuthName “Restricted access”
\nAuthUserFile \/home\/USERNAME\/.htpasswd.members
\nrequire valid-user<\/p>\n
\nAuthName “Restricted access”
\nAuthUserFile \/home\/USERNAME\/.htpasswd.admins
\nrequire valid-user<\/p>\n
\nAuthName “Restricted access”
\nAuthUserFile \/home\/USERNAME\/.htpasswd.board
\nrequire valid-user<\/p>\n
\n# htpasswd -c .htpasswd.admins adminone
\n# htpasswd -c .htpasswd.board boardmemberone<\/p>\n