{"id":6981,"date":"2024-09-13T09:00:43","date_gmt":"2024-09-13T09:00:43","guid":{"rendered":"https:\/\/www.smarthost.au\/blog\/?p=6981"},"modified":"2024-09-13T09:00:43","modified_gmt":"2024-09-13T09:00:43","slug":"how-to-restrict-access-to-a-file-folder-with-authentication","status":"publish","type":"post","link":"https:\/\/www.smarthost.au\/blog\/how-to-restrict-access-to-a-file-folder-with-authentication","title":{"rendered":"How to Restrict Access to a File\/Folder with Authentication"},"content":{"rendered":"\n<p>Sometimes, to prevent unauthorized access to a folder or file, it is necessary to add an authorization lock where access to the resource will only be granted after entering the correct username and password.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Securing Access to a Folder<\/h2>\n\n\n\n<p>Access to a folder using authorization credentials can be set up in two ways:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Setting up a folder access lock via cPanel<\/h3>\n\n\n\n<p>To set up protection via cPanel, go to cPanel -&gt; Directory Privacy.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.smarthost.eu\/blog\/wp-content\/uploads\/2024\/09\/1.png\" alt=\"\" class=\"wp-image-11256\"\/><\/figure>\n\n\n\n<p>After navigating, all the folders located in the home directory of the account will be displayed. To block access to a website folder, first go to public_html by clicking on the folder name (see the image below), and then click <strong>Edit<\/strong> next to the selected domain folder (for the purposes of this guide, we will add a lock to the folder of the <strong>client-domai<\/strong>n).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.smarthost.eu\/blog\/wp-content\/uploads\/2024\/09\/2-1024x171.png\" alt=\"\" class=\"wp-image-11260\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"346\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/3a-1024x346.png\" alt=\"\" class=\"wp-image-6982\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/3a-1024x346.png 1024w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/3a-300x101.png 300w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/3a-768x260.png 768w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/3a-520x176.png 520w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/3a-740x250.png 740w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/3a.png 1106w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After clicking <strong>Edit<\/strong>, check the box &#8220;Password protect this directory&#8221; and click <strong>Save<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"358\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/4a-1024x358.png\" alt=\"\" class=\"wp-image-6984\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/4a-1024x358.png 1024w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/4a-300x105.png 300w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/4a-768x269.png 768w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/4a-520x182.png 520w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/4a-740x259.png 740w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/4a.png 1198w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After saving and returning to this page, you will be able to enter a username and password. After entering the credentials, click <strong>Save<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"624\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/5a-1024x624.png\" alt=\"\" class=\"wp-image-6986\" srcset=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/5a-1024x624.png 1024w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/5a-300x183.png 300w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/5a-768x468.png 768w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/5a-520x317.png 520w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/5a-740x451.png 740w, https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/09\/5a.png 1197w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The folder lock has been set up. Now, when you access the site, a login window will appear:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.smarthost.eu\/blog\/wp-content\/uploads\/2024\/09\/6-5.png\" alt=\"\" class=\"wp-image-11296\"\/><\/figure>\n\n\n\n<p><strong>NOTE<\/strong><\/p>\n\n\n\n<p>If, after accessing the site, a 404 error appears instead of the login window, add the following line to the .htaccess file:<\/p>\n\n\n\n<p><code>ErrorDocument 401 default<\/code><\/p>\n\n\n\n<p>After adding this line, the login page should display correctly.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Setting up a folder access lock via .htaccess<\/h3>\n\n\n\n<p>An alternative way to set up this protection is to add rules to the .htaccess file and create a password file. To do this:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to the application folder and edit the .htaccess file.<\/li>\n\n\n\n<li>Add the following lines to the .htaccess file:<br><code>AuthType Basic<br>AuthName \"Protected 'This is a folder lock'\"<br>AuthUserFile \"\/home\/user\/.htpasswds\/public_html\/client-domain\/passwd\"<br>Require valid-user<\/code><br><br>In <strong>AuthUserFile<\/strong>, specify the path to the password file (passwd), which will be created in the following steps.<\/li>\n\n\n\n<li>After adding the record to .htaccess, create a passwd file with the login and hashed password in the path defined in the previous step (AuthUserFile). In the file, it should look like this:<br><code>login:passwordHash<\/code><br>The hash can be generated e.g. via <a href=\"https:\/\/onlinephp.io\/password-hash\">https:\/\/onlinephp.io\/password-hash<\/a><\/li>\n<\/ol>\n\n\n\n<p>After adding the above entries, when accessing the locked directory, a login page should appear where you need to enter the previously defined username and password.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.smarthost.eu\/blog\/wp-content\/uploads\/2024\/09\/6-4.png\" alt=\"\" class=\"wp-image-11292\"\/><\/figure>\n\n\n\n<p><strong>NOTE<\/strong><\/p>\n\n\n\n<p>If, after accessing the site, a 404 error appears instead of the login window, add the following line to the .htaccess file:<\/p>\n\n\n\n<p>ErrorDocument 401 default<\/p>\n\n\n\n<p>After adding this line, the login page should display correctly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Securing Access to a File<\/h2>\n\n\n\n<p>To secure access to a file only after authorization, you need to add rules to the .htaccess file and create a password file. To do this:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to the application folder and edit the .htaccess file.<\/li>\n\n\n\n<li>Add the following lines to the .htaccess file:<br><code>&lt;Files file_to_block.php><br>AuthType Basic <br>AuthName \"My file access lock\"<br>AuthUserFile \/home\/user\/.htpasswds\/public_html\/client-domain\/.htpasswd <br>Require valid-user<br>&lt;\/Files><\/code><br><br>Replace <strong>file_to_block.php<\/strong> with the name of the file you want to protect. In <strong>AuthUserFile<\/strong>, specify the path to the password file (passwd), which will be created in the following steps.<\/li>\n\n\n\n<li>After adding the record to .htaccess, create a passwd file with the login and hashed password in the path defined in the previous step (AuthUserFile).<\/li>\n<\/ol>\n\n\n\n<p>After adding the above entries, when accessing the locked directory, a login page should appear where you need to enter the previously defined username and password.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.smarthost.eu\/blog\/wp-content\/uploads\/2024\/09\/6-3.png\" alt=\"\" class=\"wp-image-11288\"\/><\/figure>\n\n\n\n<p><strong>NOTE<\/strong><\/p>\n\n\n\n<p>If, after accessing the site, a 404 error appears instead of the login window, add the following line to the .htaccess file:<\/p>\n\n\n\n<p><code>ErrorDocument 401 default<\/code><\/p>\n\n\n\n<p>After adding this line, the login page should display correctly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, to prevent unauthorized access to a folder or file, it is necessary to add an authorization lock where access to the resource will only be granted after entering the correct username and password. Securing<a class=\"read-more\" href=\"https:\/\/www.smarthost.au\/blog\/how-to-restrict-access-to-a-file-folder-with-authentication\">Continue reading<\/a><\/p>\n","protected":false},"author":26,"featured_media":7032,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[265,8],"tags":[1244,1241,1242,1248,1245,1247,1246],"class_list":["post-6981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hosting-hr","category-security","tag-access-after-authorization","tag-access-lock","tag-directory-privacy","tag-file-protection","tag-htpasswd","tag-privacy-protection","tag-protection"],"_links":{"self":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/6981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/comments?post=6981"}],"version-history":[{"count":1,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/6981\/revisions"}],"predecessor-version":[{"id":6987,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/6981\/revisions\/6987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/media\/7032"}],"wp:attachment":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/media?parent=6981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/categories?post=6981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/tags?post=6981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}