If the Apache web server is used to operate sites, it is possible to restrict access using a file.htaccess, which applies either to one directory or to all nested subdirectories. There are several types of access restrictions that may be useful in different cases. Let’s look at these possible options:

IP address restriction

The design of such a restriction is very simple:

order allow,deny
deny from 1.1.1.1
deny from 2a01:230::6d0a:8406:c4e4:9e19
deny from 10.10
deny from 192.168.0.0/24
deny from domain.dom
allow from all

System requirements for these rules to work: Apache 2.2.x web server with the mod_authz_host module enabled.

In this example, I have given all possible variations of access restrictions. And below we will analyze each line separately.

The first line sets the logic of the subsequent rules. In which order the blocking and permissive rules will be executed.

The second and third lines block access to specific IP addresses (ipv4 and ipv6, respectively).

The fourth line shows that you can specify a partial IP address, and in this case (as indicated in the example) it will be treated as a /16 subnet.

The fifth line is that you can block entire networks and subnets by specifying a range.

For the blocking options to work, as in the sixth line, you will need to enable the HostnameLookups parameter.

And finally, the last line of allow from all says that everyone else is allowed access.

Although Apache 2.2.x has been deprecated, it is still frequently used. The rules will be different for Apache 2.4.x:

<RequireAll>
Require all granted
Require not ip 1.1.1.1
Require not ip 2a01:230::6d0a:8406:c4e4:9e19
Require not ip 10.10
Require not ip 192.168.0.0/24
Require not host domain.dom
</RequireAll>

The first and last lines are directives, used to highlight a group of rules.

The second line is an analog of the order allow,deny + allow from all block in Apache 2.2. It sets the logic of the rules.

Lines 3-6 are similar to lines 2-5 of the Apache 2.2 rules, and line 7 is analogous to line 6 in Apache 2.2.

Password restriction

Let’s consider the simplest way to restrict access with login and password authorization.:

AuthType Basic
AuthName "Access is limited by the password "
AuthUserFile "/path/to/.htpasswd"

System requirements for this design: Apache 2.2.x or 2.4.x web server with connected modules auth_basic, authn_file.

Password generation

The creation of a login-password pair takes place with the direct participation of the htpasswd application, which is included in the Apache distribution package. When running without parameters, this utility will display help, the translation of which I have below.:

Using:
        htpasswd [-cmdpsD] passwordfile username
        htpasswd -b[cmdpsD] passwordfile username password

        htpasswd -n[mdps] username
        htpasswd -nb[mdps] username password

 -c Create a new file.
 -n Do not update the file, display the result on the screen.
 -m Encrypt the password using [[MD5]].
 -d Encrypt the password using [[CRYPT]] (default).
 -p Do not encrypt the password (plaintext).
 -s Encrypt the password using [[SHA]].
 -b Specify the password in the command line parameter.
 -D Delete the specified user.
On [[Windows]], [[NetWare]] and [[TPF]] systems, the '-m' flag is used by default.
The '-p' flag may not work on all other systems.

Examples of htpasswd operation

Thus, to create a file with a username and password for authorization with an encrypted password in MD5, you will need to run the command:

htpasswd -bcm /path/to/.htpasswd sampleuser samplepass

Adding password for user sampleuser

The result will be a file.htpasswd with a string:

sampleuser:$apr1$i5Kx715D$qkWbaZLztXohMGOdfCUy5/

To add a user to an existing file, run the command

htpasswd -bm /path/to/.htpasswd sampleuser1 samplepass1

Updating password for user sampleuser1

If the file contains more than one user and you need to delete one, the command will look like this:

htpasswd -D /path/to/.htpasswd sampleuser2

Deleting password for user sampleuser2

Cheat sheets

Some more examples that may be useful when working with .htaccess.

Ban everything in Apache 2.2.x:

Order deny,allow
Deny from all

Ban everything in Apache 2.4.x:

Require all denied

Blocking bots in Apache 2.2.x:

SetEnvIfNoCase User-Agent «DotBot» bad_bot
Deny from env=bad_bot

Blocking bots in Apache 2.4.x:

<If «%{HTTP_USER_AGENT} =~ m#DotBot#i»>
Require expr %{HTTP_USER_AGENT} != ‘DotBot’

#DotBot is one example, there may be many such bots.

Allow access to a specific file from a specific IP in Apache 2.2.x:

<Files path to file>
Order Deny,Allow
Deny from all
Allow from 1.1.1.1
</Files>

Allow access to a specific file from a specific IP in Apache 2.4.x:

<Files path to file>
Require ip 1.1.1.1
</Files>