Skip to content

Instantly share code, notes, and snippets.

@lokesh-webonise
Last active September 28, 2021 09:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lokesh-webonise/5625636 to your computer and use it in GitHub Desktop.
Save lokesh-webonise/5625636 to your computer and use it in GitHub Desktop.
Exclude specific folder from password protected directory area in Apache

Exclude specific folder from password protected directory area in Apache

  1. Assume your directory structure is /var/www/dir1/dir2/dir3

  2. Now your directory dir1 should be password protected & directory dir3 should be unprotected. So do this follow below mentioned steps.

Steps 1. Create virtualhost for password protected dir

<VirtualHost *:80>
    ServerName crucible.crucibletesting.com
    DocumentRoot "/var/www/dir1"
    <Directory "/var/www/dir1">
            Allow from all
            AllowOverride All
            Options Indexes FollowSymLinks
            AuthType Basic
            AuthName "password protected area"
            AuthUserFile /etc/apache2/.htpasswd
            Require valid-user
    </Directory>
</VirtualHost>

Steps 2. Now add directory tag for unprotected directory in above virtual host

<Directory  "/var/www/dir1/dir2/dir3">
            # All access controls and authentication are disabled
            # in this directory
            Order Deny,Allow
            Allow from all
            Satisfy any
</Directory>

After adding above tag, virtualhost will be look like this.

<VirtualHost *:80>
    ServerName crucible.crucibletesting.com
    DocumentRoot "/var/www/dir1"
    <Directory "/var/www/dir1">
            Allow from all
            AllowOverride All
            Options Indexes FollowSymLinks
            AuthType Basic
            AuthName "password protected area"
            AuthUserFile /etc/apache2/.htpasswd
            Require valid-user
    </Directory>
    <Directory  "/var/www/dir1/dir2/dir3">
            # All access controls and authentication are disabled
            # in this directory
            Order Deny,Allow
            Allow from all
            Satisfy any
    </Directory>

</VirtualHost>
@Rohlik
Copy link

Rohlik commented May 5, 2020

Great how-to article. Just a small point, order does matter ;-)

@Eddcapone
Copy link

Does it also work with .htaccess? I dont have access to the VHOST config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment