Drupal: .htaccess Access Deny
In this article I will tell how to forbid access to certain resources for some clients. The instructions will include descriptions of different directives.
First let’s speak about Order directive of .htaccess file of Apache server.
Description: Sets order of work of Deny and Allow directives. Order can take the following values: [Deny , Allow] or [Allow , Deny].
Syntax: the default state:
Order Deny,Allow
Deny directive
Description: Denies access to certain resources for specified clients.
Syntax:
Deny from all|host
host can take values of IP address or host name.
Allow directive
Description: Allows access to certain resources for specified clients.
Syntax:
Allow from all|host
host can take values if IP address or host name.
Example: .htaccess access deny for all clients
Order Deny,Allow
Deny from all
Example: .htaccess access_ allow_ for a certain IP address and host.
Order Deny,Allow
# access deny to a resource for all clients
Deny from all
# allow for the following IPs only
Allow from local.
Allow from 100.99.99.1, 100.99.99.2
Example: .htaccess access_ deny_ for a certain IP address and host.
Order Allow,Deny
#allow all users to login to the server
Allow from all
Deny from local.
Deny from 100.99.99.1, 100.99.99.2
If you need to deny access for entire network (100.99.99.1 – 100.99.99.255) you should set a combination of the first lems: 100.99.99
Files directive
Description: : denies access to a certain file.
Example: access deny to a file with passwords (.htpasswd) for all users except 100.99.99.1 и 2
<Files “.htpasswd”>
Order Deny,Allow
Deny from all
Allow from 100.99.99.1, 100.99.99.2
</Files>
In the example below, we will deny entering to the resource for users whose IPs are 100.99.99.1, 100.99.99.2 using GET method (.htaccess ip):
<Limit GET>
Order Allow,Deny
Deny from 100.99.99.1, 100.99.99.2
Allow from All
</Limit>
We denied user with IP = 100.99.99.1 viewing the site. If we replace 100.99.99.1 with 100.99.99, access will be denied for the entire network of C class.
Here is one more example: access deny to a group of files. In the example below, access is denied to files with “lib” and “pm” extensions and for all users except 100.99.99.1
<FilesMatch “\.(lib|pm)$”>
# или например : <FilesMatch “\.(gif|jpe?g|png)$”>
Order Deny,Allow
Deny from all
Deny from 100.99.99.1
</FilesMatch>
That’s all for now.
Good luck!