Allow public access to a file in a protected directory using .htaccess

Within your website structure, there are many times when you may want to protect directories from outside access. This prevents any user from accessing a sub-directory of your site, and its files, without a username and password. There may be a time, however, when you want to allow access to a single file within a password directory and it is not feasible to move the file to an unprotected directory or folder. The instructions below will guide you in how to allow access to a single file in a password protected directory using your .htaccess file.

How to allow access to a file in a protected directory

Step 1. Log into your cPanel dashboard.
Step 2. Access the File Manager under the Files section and navigate to the password protected folder. Make sure you enable the ability to show hidden files by clicking Settings at the top right and clicking the check box for Show Hidden Files (dotfiles).

Step 3. Within the password protected folder will be an .htaccess file that contains the security code to activate the password popup. Highlight the .htaccess file and click on the Edit icon from the toolbar across the top of the page.

Step 4. Once inside the editor, you will see the security code. It will appear similar to the example below. Our example directory was test, so attempting to access resulted in a popup box asking for username and password.

AuthName "test-protect"
AuthUserFile "/home/userna5/.htpasswds/public_html/test/passwd"
AuthType Basic
require valid-user

You will need to add the following code for each file you want to allow access to after the code above. In this example, we are allowing access to a file named test.php. Note the \ before the . in the filename and the $ at the end. You will want to do this for your file name as well to ensure access is given to that exact file.

Satisfy Any
Allow from all

Step 5. Click on the Save Changes button in the upper right corner to finalize and activate the code. Now you will be able to access the specific file named test.php but no other files within that directory.

  • public access, protected directory, .htaccess
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to Force www or non-www in htaccess

Many customers at some point request to force either the www or non-www version of their site to...

How to Change your Default Index Page in .htaccess

The server looks for specifically named files as the first page of your website, also known as...

How to Add mod_expires to your .htaccess

When visiting a website, the website will cache in your browser. This cache can be controlled by...

Where is my .htaccess File?

In this article we will show you where your account's .htaccess file resides on the server, as...

Removing file extensions using htaccess

Lately, there seems to be a trend where designers want to cloak their file extensions from their...