In this article, I will tell you how to password protect a subdirectory when you have WordPress installed in your root directory.
If you host your website on Hostgator or another leading provider that uses cPanel, you see a choice in your cPanel that allows you to password protect a subdirectory. This works great…unless you have WordPress installed in the root of your domain with Permalinks enabled (which you should always have for SEO benefits.)
I write this solution today as much for me as for you. I had implemented this solution a year ago but was having trouble remembering the little steps to take to make it happen.
You will find information on this topic on Hostgator here and another article here, but I prefer the approach I outline below because it works and does not require modifying the .htaccess file in your root WordPress directory.
Step 1: Password Protect Your Subdirectories Through cPanel
Here are the instructions from Hostgator. Perform these steps first but don’t test them yet as you need to perform steps 2 and 3 next.
Step 2: Create a 401.HTML File
Create a file named 401.html in your favorite text editor that says only: “PASSWORD PROTECTED FOLDER – Please enter the correct username/password.” Upload this file to the root of your domain (under public_html).
Step 3: .HTACCESS File for SubDirectory
Create a file name .htaccess with the content below and place it in your subdirectory:
.htaccess File for SubDirectory
ErrorDocument 401 /401.html AuthName "directoryname" AuthUserFile "pathtoppassworddirectory" (with HostGator the path is as follows: "/home/username/.htpasswds/public_html/directoryname/passwd" AuthType Basic require valid-user
Password Protect SubDirectory When WordPress Is Installed: Conclusion
You will find several solutions to password protecting a subdirectory when WordPress is installed. I prefer the three-step method that I demonstrated above because it does not require you to modify other existing, active files.
I hope this information has helped those out there who are researching this issue. I know that it took me time to figure it out (twice) and it probably has taken you some time to research this as well.
Hopefully, I have saved you some time.
Thanks for this article. I tried a half a dozen things that were similar to this method but didn’t work. This worked perfectly. Very easy to set up. Thanks.
Thanks for this article, it’s been really helpful.
In order for this to work do you have to password protect the sub-directories in Cpanel? Would this solution work without doing this?