How to Whitelist Directory in Apache


whitelist directory in apache

Sometimes, you may need to whitelist directories and subfolders in an otherwise protected website. Let us take a look at how to whitelist directory in Apache.

 

How to Whitelist Directory in Apache

Here are the steps to whitelist directory in Apache.

Before proceeding, please ensure you have enabled .htaccess (mod_rewrite) in your Apache web server. Here are the steps to do it:

 

Place your .htaccess file in the root document folder of your website (/var/www/html)

 

1. Open .htaccess file

Open .htaccess file using a text editor. It is generally located at /var/www/html.


$ sudo vim /var/www/html/.htaccess

 

2. Whitelist Directory

Let’s say all pages on your website/portal are password protected/restricted and you want to allow access to all for a specific directory (e.g /downloads/). In that case, add the following code to your .htaccess file.

Apache 2.2


<Directory /downloads/>
# All access controls and authentication are disabled
# in this directory
Satisfy Any
Allow from all
</Directory>

Apache 2.4+


<Directory /downloads/>
# All access controls and authentication are disabled
# in this directory
Require All Granted
</Directory>

 

The above code will enable access to all to the specified directory.

 

If you want to allow access only to specific IPs like 127.0.0.1 or range of IPs such as 192.0.0.1-192.255.255.255, you can specify them in Allow/Require directives

Apache 2.2


<Directory /downloads/>
# All access controls and authentication are disabled
# in this directory
Satisfy Any
Allow from 127.0.0.1
Allow from 192.0.0.0/24
</Directory>

Apache 2.4+


<Directory /downloads/>
# All access controls and authentication are disabled
# in this directory
Require 127.0.0.1 Granted
Require 192.0.0.0/24 Granted
</Directory>

 

 

3. Restart Apache web server

Restart Apache web server to apply changes


$ sudo /etc/init.d/apache2 start [Debian or Ubuntu]
# sudo apachectl restart [RHEL, CentOS or Fedora]

 

That’s it! Users wil now have access to the specific directory you have mentioned.

 

 

 

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!