By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Only Allow Specific IPs With Htaccess

How to block everyone except your home or work IP using htaccess.

Edited: 2012-06-28 03:36

Sometimes you want to only allow access from certain IPs, such as when having an admin only section on your site – denying access from all is easily controllable with .htaccess files on a per directory basis.

The below will limit all the get and post requests within a given directory, and all its subdirectories, you will need to place this in a file named .htaccess in the directory that you want to limit access to.

<Limit GET POST>
order deny,allow
deny from all

# Allow Home IP
allow from

# Allow Work IP
allow from

The first line controls the order of the rule, setting this to deny,allow means that you will first have to write in the addresses that should be blocked – this can be an IP-range, a single ip, or a keyword. The All keyword was used in this example, and it will simply block access from all IPs, except for the ones that you type in. Simply replace with your own IP, unless you are working on your LAN this should be your WAN IP.