The rule
Say you want to allow only the IP address 1.2.3.4. Add this to the .htaccess file of your site, or of the folder you want to protect (Apache 2.4):
Require ip 1.2.3.4
Everyone else gets a 403 Forbidden page.
For older servers (Apache 2.2 style)
Order Deny,Allow
Deny from All
Allow from 1.2.3.4
Order Deny,Allowsets the order in which the rules are processed.Deny from Allblocks every address first.Allow from 1.2.3.4then lets your address in.
Note: If your server runs Apache 2.4, use
Require ip. The old Order, Allow and Deny lines work only when the compatibility module is loaded. Do not mix both styles in one file.Allow more than one address
Require ip 1.2.3.4 5.6.7.8
You can also use a range, for example Require ip 1.2.3.0/24.
Test and undo
- Save the file and open the site from the allowed IP address. It should load.
- Open it from another network. It should show a 403 error.
- To undo, delete the lines you added.
Warning: Check your own IP address before you save. If you use a wrong address, you lock yourself out. If your IP changes often, this method is not the best for you.
Quick recap
Require ipallows only the listed addresses (Apache 2.4).- Everyone else gets a 403 error.
- Make sure your own IP is right before you save.