htaccess allow IP

Using .htaccess will let you control who is able to access and view different areas of your website. Continue reading to learn how to allow site access based on an IP address.

Website Access

order deny,allow
deny from all
# Jordan's computer
allow from 1.2.3.4
# Kate's computer
allow from 5.6.7.8

You may restrict access to your website so that only the IP addresses you specify can view your website. In the sample above everyone is blocked from visiting my website except for my computer (IP Address: 1.2.3.4) and my friend Kate’s computer (IP Address: 5.6.7.8). Copy the code above and replace the IP addresses that you want to give access to. Place the .htaccess at the root level of your website.

File Access

<Files CheatCodes.php>
order deny,allow
deny from all
# Jordan's computer
allow from 1.2.3.4
# Jordan's laptop
allow from 5.6.7.8
</Files>

The sample above demonstrates how you can restrict access to a particular file on your website. In the sample I am limiting access to the CheatCodes.php file so that only my compter and my laptop are able to view the file. You have several options for placing the code in your .htaccess file. You can place the code in your site’s root .htaccess or create a new .htaccess in the CheatCodes.php’s directory. Remember to make sure that the file path is correct if you are placing the .htaccess outside of your file’s location.

Folder Access

order deny,allow
deny from all
# Jordan's computer
allow from 1.2.3.4

This is very similar to the website access mentioned earlier in the article. Instead of placing the code shown above in your website’s root folder, you will now be creating a new .htaccess and place it in the directory that you want to limit access to. Let’s say that you have a region of your site that you wish to block access to and the region is: website.com/testing/. You will now create a new .htaccess file and place it at /testing. The permissions granted in the .htaccess will apply to all sub directories in /testing so you will not have to worry about creating subsequent .htaccess files for other folders in that directory.

You may be interested in blocking access from an IP address