htaccess block IP

Using .htaccess will let you control who is able to access and view different areas of your website. Learn how to block access to an IP address in this article.

Website Access

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

You may prevent people from accessing your website by blocking their IP address. In the sample above, the .htaccess file will allow everyone to visit the website except for two IP addresses. The two IP addresses are mine (1.2.3.4) and my friend Kate’s (5.6.7.8). Copy the code above and replace the IP addresses that you want to block access from. Place the .htaccess at the root level of your website.

File Access

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

The sample above demonstrates how you can restrict access to a particular file on your website. In the sample above, everyone is given access to view the CheatCodes.php file except for my brother Garrett. You have several options for placing the .htaccess file on your website. You can place it at your site’s root .htaccess or you can place it in the CheatCodes.php’s file location. Just make sure that you specify the file path if you are placing the code outside of your CheatCode.php file’s directory. ex)

<Files /games/fps/CheatCodes.php>

Folder Access

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

This is very similar to how you block an IP address from accessing our website. Instead of placing the code shown above in your website’s root folder, you will be creating a new .htaccess file and placing it in the directory that you wish to block users from accessing. The .htaccess rules will apply to all sub directories so you will not have to worry about creating subsequent .htaccess files for any new folders.

You may be interested in allowing access from an IP address