Password Protect public_html

  1. Log in to cPanel
  2. Click Directory Privacy
  3. Click public_html
  4. Select Password protect this directory
  5. Enter a name, for example, website in the Enter a name for the protected directory field
  6. Click Save
  7. Click Go back

Create a user

  1. Enter a username in the Username field under Create User
  2. Enter a password in the New Password field
  3. Click Save

Advanced Tips

Set a 401 Error Document

  1. Log in to cPanel
  2. Click File Manager
  3. Open public_html, select .htaccess and click edit
  4. Find the AuthType Basic code:
         AuthType Basic
         AuthName "Dev Site"
         AuthUserFile "/home/user/.htpasswds/public_html/passwd"
         require valid-user
         
  5. Add ErrorDocument 401 “Unauthorized” as below:

        AuthType Basic
        AuthName "Dev Site"
        AuthUserFile "/home/user/.htpasswds/public_html/passwd"
        require valid-user
        ErrorDocument 401 "Unauthorized"
        

Allow server access to your protected website

  1. Log in to cPanel
  2. Click File Manager
  3. Open public_html, select .htaccess and click edit
  4. Find the AuthType Basic code:

        AuthType Basic
        AuthName "Dev Site"
        AuthUserFile "/home/user/.htpasswds/public_html/passwd"
        require valid-user
        ErrorDocument 401 "Unauthorized"
        

  5. Add RequireAny Require ip options as below:

        AuthType Basic
        AuthName "Dev Site"
        AuthUserFile "/home/user/.htpasswds/public_html/passwd"
        <RequireAny>
            Require ip 127.0.0.1
            Require valid-user
        </RequireAny>
        ErrorDocument 401 "Unauthorized"