How To Block IP Address from Accessing Your Website with .htaccess
Ever wanted to block someone from your website? You can setup blocks for a single IP, or a range. Maybe it’s a bot that is putting your limits though the roof, or your grandmother for some reason. Note that someone could easily use a VPN, or a different Wi-Fi network to get around this, so don’t use it to ban tech savvy people as it won't work.
All you have to do is add this code into your .htaccess file and save it! Like all file changes, it can take some time before it updates everywhere, but you can speed up the process on your devices simply by clearing your browser’s cache. This code snippet will block IP(s) from accessing your website!
Block singular IP Addresses
<RequireAll> Require all granted Require not ip [IP #1] [IP #2] [IP #3] </RequireAll>
You can add as many IPs as you want, just make sure to separate each with a new line. See below for how to block a range or IPs.
Block a range of IP addresses
<RequireAll> Require all granted Require not ip 10.252.*.* Require not ip 192.168.1.0/24 #Block a range of IPs </RequireAll>
Unsure of how to install this code? It's pretty simple. A .htaccess file is called “.htaccess”, with nothing before the dot. Try finding this on your FTP software. Many systems don’t allow filenames starting with a dot (So downloading a htaccess file can be difficult at times), so that also restricts how we can create a .htaccess file. It is possible to get around these restrictions though, so here are the steps!
- Open your text-editing program and code your .htaccess file
- Once you are finished, save it as “htaccess.txt” (We will fix its name later)
- Upload “htaccess.txt” to your webserver and rename it to “.htaccess” (Remember that the dot is important!)
If you are still confused, or it's not working for you, check out the more detailed instruction in our Finding, Creating, and Editing a .htaccess file article.
Hopefully you were successful in installing this snippet, if you need help, feel free to send us a message! Check out the rest of the htaccess snippets to see what other amazing things you can do!