Learn how to use .htaccess files to restrict access to specific directories based on IP addresses.

This technique is particularly useful when you want to limit access to sensitive areas of your website to a select few IP addresses, thereby enhancing your site’s security.

Steps to create a .htaccess file to restrict folder access to a specific IP address

  1. Open a text editor (like Notepad, TextEdit).
  2. Paste the following code into your text editor:
        order deny,allow
        deny from all
        allow from YOUR_IP_ADDRESS
        

    Replace YOUR_IP_ADDRESS with the IP address that you want to allow access.

  3. Save this file as ‘.htaccess’.
  4. Upload the ‘.htaccess’ file to the directory you want to restrict access to.

Please note that this will deny access to all IP addresses except the one you specified.

Remember to replace YOUR_IP_ADDRESS with the actual IP address you want to allow. For example, if you want to allow the IP address 192.168.1.1, the code would look like this:

order deny,allow
deny from all
allow from 192.168.1.1

This will restrict access to the directory to only the IP address 192.168.1.1. All other IP addresses will be denied access.

How to allow access to multiple IP addresses

Add additional ‘allow from YOUR_IP_ADDRESS’ lines to allow multiple IP addresses to access your directory.

order deny,allow
deny from all
allow from IP_ADDRESS_ONE
allow from IP_ADDRESS_TWO
allow from IP_ADDRESS_THREE

Please be aware that IP addresses can change, so you may need to update this file if your IP address changes. If you’re trying to allow access to a dynamic IP, you might need to consider a different method of restricting access.

Also, please be careful when editing .htaccess files as incorrect configurations can cause issues with your website. Always make a backup before making changes.