Sometimes you may need to make a folder or subfolder inaccessible, or block access to a specific subdirectory on your website. In this article, we will look at how to make directory inaccessible with .htaccess for Apache web server. You can use it to control access to folders and directories that contain sensitive information.
How to Make Directory Inaccessible with .htaccess
Here are the steps to make directory inaccessible with .htaccess. Let us say you want to deny access to /data/ folder.
1. Create .htaccess
Navigate to the folder that you want to make inaccessible.
$ sudo cd /var/www/html/data
Create an .htaccess file in this directory.
$ sudo vi .htaccess
Also read : How to Remove index.php from URL using .htaccess
2. Block access to directory
Add the following line to block access to directory from all IPs.
deny from all
If you want to allow access from specific IP, add the following line. Replace [your IP] with the IP from which you want to allow access.
allow from [your IP]
Save and close the file.
Also read : Apache Config File Location
3. Restart Apache web server
Restart Apache server to apply changes.
$ sudo service apache2 restart
That’s it. Now you if you open browser and try accessing http://your-domain.com/data from any IP address other than the one you have allowed access, you will get a “403: Access Forbidden” message.
Also read : How to Redirect HTTP to HTTPS in Apache