.htaccess is a configuration file for the Apache web server. It’s an extremely powerful tool that can be used to modify the Apache configuration without editing the Apache configuration files directly. Let us see how to create this configuration and use it to restrict directory listings and IP addresses, and to handle redirects.
Enable .htaccess file:
sudo nano /etc/apache2/sites-available/test.com.conf
After the VirtualHost block () add:
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Save the file and restart apache server.
sudo service apache2 restart
Go to below path and create .htaccess file
cd /var/www/html/test.com/public_html
Block Ips:
order allow,deny
# This will deny the IP 192.0.2.9
deny from 192.0.2.9
# This will deny all IP's from 192.0.2.0 through 192.0.2.255
deny from 192.0.2
Allow Ips:
order deny,allow
# Denies all IP's
Deny from all
# This will allow the IP 192.0.2.9
allow from 192.0.2.9
# This will allow all IP's from 192.0.2.0 through 192.0.2.255
allow from 192.0.2