{"id":6291,"date":"2021-05-21T15:48:28","date_gmt":"2021-05-21T15:48:28","guid":{"rendered":"https:\/\/oxhosting.com\/blog\/2021\/05\/21\/how-to-password-protect-files-and-directories-hosting\/"},"modified":"2021-05-21T15:48:28","modified_gmt":"2021-05-21T15:48:28","slug":"how-to-password-protect-files-and-directories-hosting","status":"publish","type":"post","link":"https:\/\/oxhosting.com\/blog\/2021\/05\/21\/how-to-password-protect-files-and-directories-hosting\/","title":{"rendered":"How to password protect files and directories &#8211; Hosting"},"content":{"rendered":"<p> <script data-ad-client=\"ca-pub-3214842754935876\" async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<\/p>\n<div id=\"article\">&#13;<br \/>\n\t\t<b>&#13;<br \/>\n\t\t<\/b>In this guide you can find how to set up password protection both for directories and a single file.<br \/><b><\/b><br \/>How to password protect directories<br \/>How to password protect files\u00a0<br \/><b>How to password protect directories<\/b><\/p>\n<p>1. Log in to your cPanel account &gt;&gt; <b>Files<\/b> section &gt;&gt; <b>Directory Privacy<\/b> menu:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>2. Once there, you may pick up the directory you wish to protect:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_819_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>3. In <b>Security Settings<\/b> section give a name to the protected directory\u00a0 and check-mark <b>Password protect this directory<\/b> option:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_465_How-to-password-protect-files-and-directories-Hosting.png\"\/><br \/><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_807_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>4. In <b>Create User<\/b> section specify the username and the password that should be used to access this directory:&#13;\n<\/p>\n<div><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_983_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/div>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_86_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>5. In our example we&#8217;ve set protection for <i>wp-admin<\/i> directory, so that when following <i>http:\/\/nctest.info\/wp-admin<\/i> link, the following authentication pop-up window comes up:&#13;\n<\/p>\n<p><b>Mozilla Firefox<\/b><\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_541_How-to-password-protect-files-and-directories-Hosting.png\"\/><br \/><b><br \/>Google Chrome<\/b><\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_699_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>This error message will appear if one fails to enter the required credentials:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_779_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<div>1. Log in to your cPanel account &gt; <b>Files<\/b> section &gt; <b>Directory Privacy<\/b> menu:<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_712_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>2. Once there, you may pick up the directory you wish to protect:&#13;<br \/>\n&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_141_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>3. In <b>Security Settings<\/b> section give a name to the protected directory\u00a0 and check-mark <b>Password protect this directory<\/b> option:&#13;<br \/>\n&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_749_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<\/div>\n<div><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_750_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>4. In <b>Create User<\/b> section specify the username and the password that should be used to access this directory:&#13;<br \/>\n&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_871_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<\/div>\n<div><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_131_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>5. In our example we&#8217;ve set protection for <i>wp-admin<\/i> directory, so that when following <i>http:\/\/nctest.info\/wp-admin<\/i> link, the following authentication pop-up window comes up:&#13;<br \/>\n&#13;\n<\/p>\n<p><b>Mozilla Firefox<\/b><\/p>\n<\/div>\n<div><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_541_How-to-password-protect-files-and-directories-Hosting.png\"\/><br \/><b><br \/>Google Chrome<\/b><\/div>\n<div><b>\u00a0<\/b><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_699_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>This error message will appear if one fails to enter the required credentials:&#13;<br \/>\n&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_514_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<\/div>\n<p><b>How to password protect files<br \/><\/b><br \/>Follow the steps below in order to password protect an individual file:&#13;<\/p>\n<p>1. Navigate to <b>\/home\/user<\/b> in your <b>File Manager<\/b>:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_744_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>Make sure that <b>Show Hidden Files (dotfiles)<\/b> option is enabled in <b>Settings<\/b> menu:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_472_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>2. Create a file named <b>.htpasswd<\/b> and place it above your root directory, so visitors cannot access it.&#13;<br \/>\n<br \/>Example of the root directory pathway for shared accounts:<i> \/home\/cpanelusername\/.htpasswd &#13;<br \/>\n&#13;<br \/>\n<\/i><\/p>\n<p><b>NOTE:<\/b> replace <i>cpanelusername <\/i>with the actual cPanel username for your hosting account:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_134_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_714_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>3. Create an encrypted password using htpasswd generator. It doesn&#8217;t matter how many individual files you wish to protect, they can all share a common .htpasswd file:&#13;\n<\/p>\n<div><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_48_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/div>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_152_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>4. Right click on <b> .htpasswd<\/b> file and select the <b>Edit<\/b> option: &#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_38_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>5. Place the generated .htpasswd code into .htpasswd file and click on <b>Save Changes<\/b> button:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_778_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>6. Navigate to the directory where the file that you would like to password protect is stored.&#13;\n<\/p>\n<p>7. If there is still no .htaccess file, create one by clicking on <b>+File<\/b> icon:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_465_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>8. Right click on .htaccess file and select the <b>Edit<\/b> option:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_573_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>9. Insert the following code into .htaccess file: \t&#13;<br \/>\n&#13;\n<\/p>\n<pre class=\"prettyprint\">&lt;FilesMatch \"examplefile.extension\"&gt;&#13;\n<br\/>AuthName \"Member Only\"&#13;\n<br\/>AuthType Basic&#13;\n<br\/>AuthUserFile \/home\/cpanelusername\/.htpasswd&#13;\n<br\/>require valid-user&#13;\n<br\/>&lt;\/FilesMatch&gt;<\/pre>\n<p><b>NOTE: <\/b>replace <i>examplefile.extension<\/i> with your actual file&#8217;s name and <i>cpanelusername <\/i>with your actual cPanel username.&#13;<br \/>\n&#13;<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_687_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>10. Click on <b>Save <\/b>and then on <b>Close<\/b> button.&#13;<br \/>\n&#13;\n<\/p>\n<p>This will help you to protect the specified file, while leaving all other files in the folder unprotected.&#13;\n<\/p>\n<p><b>WordPress and password protected files<\/b><\/p>\n<p>WordPress can have some minor conflicts with password protecting files due to the changes in its default .htaccess rules. &#13;<br \/>\n&#13;\n<\/p>\n<p>If you have WordPress installation with enabled permalinks, it is not possible to use <b>Password Protect Directories<\/b> option in cPanel or via a .htpasswd file.&#13;<br \/>\n&#13;\n<\/p>\n<p>Following these steps you will be able to password protect files by editing .htaccess file. <br \/>Review this article for further information regarding how to edit your .htaccess file.&#13;<br \/>\n&#13;\n<\/p>\n<p><b>NOTE:<\/b> make sure that you have created a backup of your .htaccess file before making any changes to it. Thus, you will be able to revert back to a previous version if\u00a0 needed.&#13;<br \/>\n&#13;\n<\/p>\n<p>1. Edit .htaccess File&#13;<br \/>\n&#13;\n\t<\/p>\n<p>Edit .htaccess file in the folder for your WordPress installation and remove the line of code shown in bold below:&#13;\n<\/p>\n<pre class=\"prettyprint\">#BEGIN WordPress&#13;\n\t<br\/>&lt;IfModule mod_rewrite.c&gt;&#13;\n<br\/>RewriteEngine On&#13;\n\t<br\/>RewriteBase \/&#13;\n\t<br\/>RewriteCond %REQUEST_FILENAME !-f&#13;\n\t<br\/>RewriteCond %REQUEST_FILENAME !-d&#13;\n\t<br\/>RewriteRule . \/index.php [L]&#13;\n<br\/>&lt;\/IfModule&gt;&#13;\n\t<br\/>#END WordPress<\/pre>\n<p>Replace this line:&#13;<\/p>\n<pre class=\"prettyprint\">RewriteRule . \/index.php [L]<\/pre>\n<p>with this new one:&#13;<br \/>\n&#13;<\/p>\n<pre class=\"prettyprint\">RewriteRule .\/ \/index.php [L]<\/pre>\n<p>After that add the code needed for password protection. The final version should look like this:&#13;<br \/>\n&#13;<\/p>\n<pre class=\"prettyprint\">#BEGIN WordPress&#13;\n<br\/>&lt;IfModule mod_rewrite.c&gt;&#13;\n<br\/>RewriteEngine On&#13;\n<br\/>RewriteBase \/&#13;\n<br\/>RewriteCond %REQUEST_FILENAME !-f&#13;\n<br\/>RewriteCond %REQUEST_FILENAME !-d&#13;\n<br\/>RewriteRule .\/index.php [L]&#13;\n<br\/>&lt;\/IfModule&gt;&#13;\n<br\/># END WordPress&#13;\n<p>&lt;FilesMatch \"examplefile.extension\"&gt;&#13;\n<br\/>AuthName \"Member Only\"&#13;\n<br\/>AuthType Basic&#13;\n<br\/>AuthUserFile \/home\/cpanelusername\/.htpasswd&#13;\n<br\/>require valid-user&#13;\n<br\/>&lt;\/FilesMatch&gt;<\/p><\/pre>\n<p>And <b>save the changes<\/b>.&#13;<br \/>\n&#13;\n<\/p>\n<p>Below you can see the example of our .htaccess file code:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_704_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p><b>2. Reset Permalinks&#13;<br \/>\n&#13;<br \/>\n<\/b><\/p>\n<p>You will also need to re-generate WordPress permalinks to make sure that they have the new URL structure. You can do it in your WordPress admin panel &gt;&gt; <b>Settings <\/b>&gt;&gt; <b>Permalinks<\/b> menu.&#13;\n<\/p>\n<p>\u00a0If you had <b>Post name<\/b> option enabled, for instance, you need to switch it to <b>Default<\/b>, save the changes and then revert everything back: &#13;<br \/>\n&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_1_How-to-password-protect-files-and-directories-Hosting.png\"\/>\u00a0\u00a0&#13;<br \/>\n&#13;\n<\/p>\n<p>This should reset the permalinks and prevent any posts or pages from returning a 404 Page Not Found error.<\/p>\n<p>As a result, you will get the follow authentication pop-up window when trying to open a protected file in browser:&#13;<br \/>\n&#13;\n<\/p>\n<p><b>Mozilla Firefox<\/b><\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_541_How-to-password-protect-files-and-directories-Hosting.png\"\/><br \/><b><br \/>Google Chrome<\/b><\/p>\n<p>\u00a0<img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_699_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>This error message will appear if one fails to enter the required credentials:&#13;<br \/>\n&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612106_779_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p><b>How to password protect multiple files<\/b><\/p>\n<p>You can protect more than one file by using wildcard names. On the FilesMatch line in the previous .htaccess code, try one of the directives below:&#13;\n<\/p>\n<pre class=\"prettyprint\">&lt;FilesMatch \"*.html\"&gt;&#13;\n<br\/>&lt;FilesMatch \"file.*\"&gt;&#13;\n<br\/>&lt;FilesMatch \"*.*\"&gt;<\/pre>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621612107_118_How-to-password-protect-files-and-directories-Hosting.png\"\/><\/p>\n<p>That&#8217;s it!<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Need any help? Contact our HelpDesk<\/p>\n<\/div>\n<script data-ad-client=\"ca-pub-3214842754935876\" async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>&#13; &#13; In this guide you can find how to set up password protection both for directories and a single file.How to password protect directoriesHow&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/posts\/6291"}],"collection":[{"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=6291"}],"version-history":[{"count":0,"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/posts\/6291\/revisions"}],"wp:attachment":[{"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=6291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=6291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=6291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}