How to restrict particular file types in Apache directory in CentOS 6.7

To restrict particular file types in Apache directory in CentOS 6.7

In our previous article, we have explained about the configuration procedure of How to restrict Apache Directory Access in CentOS 6.7. This article will be briefed about how to restrict particular file types in Apache directory in CentOS 6.7. Apache directory contains so many file types will available to perform different task. This method is useful when it comes to allow only particular file types access for specific user or entire user.

Configuration procedure

Before proceeding to the configuration process, check the status of Apache in the target system.

[root@domain1 ]# service httpd status

Next locate the file content directory to restrict some specific files.

[root@domain1 ~]# cd /var/www/html/fold/one/
[root@domain1 one]# ls -l
total 664
drwxr-xr-x 2 root root   4096 Oct  9 00:43 f1
-rw-r--r-- 1 root root     27 Oct  9 00:42 file1.txt
-rw-r--r-- 1 root root 469423 Oct 10 00:20 lcommands.pdf
-rw-r--r-- 1 root root  11913 Oct 10 00:22 lin.png
-rw-r--r-- 1 root root 136723 Oct 10 00:20 linux.pdf
-rw-r--r-- 1 root root   5466 Oct 10 00:23 Red_Hat.jpg
-rw-r--r-- 1 root root  40612 Oct 10 00:26 ubu.jpg

Open the Apache configuration file and create rule to restrict specific files types in the directory. In this file, restrict some .pdf and .jpg file for the entire users. So files other than the restricted ones can be accessed by the user.

[root@domain1 ~]# vim /etc/httpd/conf/httpd.conf

Directory /var/www/html/fold/one> 
       order allow,deny
       allow from all
< Files ~ " .(pdf|jpg)" > 
      deny from all
< /Files> 
< /Directory> 

Restart the Apache service.

[root@domain1 ~]# service httpd restart

Switch over to the browser and check the configuration from browser by accessing the index page of the directory.

Next try to access blocked files and the access will be blocked by the Apache service.

To restrict specific files types for particular IP. Open the Apache Configuration and enter the following content in the file. Save and exit the file.

[root@domain1 one]# vim /etc/httpd/conf/httpd.conf

< Directory /var/www/html/fold/one> 
       order allow,deny
       allow from all
< Files ~ " .(pdf|jpg)" > 
      deny from 192.168.7.206
< /Files> 
< /Directory> 

Restart the Apache service.

[root@domain1 ~]# service httpd restart

Now to check the configuration procedure and the index page is displayed before setting the restriction access for client.

After setting the restriction access for client. The index page will be displayed like below.

While trying to access blocked files and the user will be forbidden to access the site.


Thus concludes the configuration procedure of restricting particular file types in Apache directory in CentOS 6.7.

Tag : Apache CentOS
FAQ
Q
What is the purpose of need two apache server for same website ?
A
if the main web server fails, the backup server will take over it in order to reduce downtime of the website.
Q
Can you please tell me if I want to change all my configuration are not set in httpd.conf, I want to save these anywhere else, how it can be possible?
A
Take the backup of apache configuration file using following command before making any changes to the file. # cp /etc/httpd/conf.d/httpd.conf cp /etc/httpd/conf.d/httpd.conf_org OR # cp /etc
Q
How do I set up Apache to require a username and password to access certain documents?
A
There are several ways to do this; some of the more popular ones are to use the mod_authn_file, mod_authn_dbd, or mod_authnz_ldap modules. For an explanation on how to implement these restri
Q
Do I have to keep the (SQL) authentication information on the same machine?
A
Some organizations feel very strongly about keeping the authentication information on a different machine than the webserver. With the mod_auth_msql, mod_auth_mysql, and other SQL modules con
Q
“Options -Indexes ” But I am not sure how the final contents of this file .htaccess should be shown: Options -Indexes AllowOverride All Is that right?
A
Simply add the line Options -Indexes in the .htaccess file and save the it.