How to Whitelist IP Address Ranges with .htaccess
.htaccess is useful for more than URL rewriting and caching. It can also provide an additional access-control layer, including restricting an application to specific IP addresses or IP ranges.
This technique can be useful when:
- An application is still in development and should only be available to an internal team.
- You want to protect sensitive paths such as
/adminor/api. - A server should only be reachable from an office network or VPN.
Whitelist One IP Address
To allow only one IP address:
<RequireAll>
Require ip 203.0.113.25
</RequireAll>With this rule, only 203.0.113.25 is allowed. Other clients receive a 403 Forbidden response.
Whitelist Multiple IP Addresses
Use <RequireAny> when more than one address should be accepted:
<RequireAny>
Require ip 203.0.113.25
Require ip 198.51.100.10
Require ip 192.0.2.8
</RequireAny>Only those three addresses are granted access.
Whitelist an IP Range with CIDR
To allow an entire range, use CIDR notation:
<RequireAll>
Require ip 203.0.113.0/24
</RequireAll>/24covers addresses from203.0.113.0through203.0.113.255.- The entire subnet is therefore included in the allowlist.
You can also combine individual addresses and ranges:
<RequireAny>
Require ip 203.0.113.25
Require ip 198.51.100.0/24
</RequireAny>Important Notes
- Apache 2.4+ → The
Requiresyntax applies to Apache 2.4 and later. Apache 2.2 usedAllow fromandDeny from. - Test first → Validate access rules in a staging environment whenever possible. A bad rule can lock you out of the application.
- Scale appropriately →
.htaccessworks well for small and medium allowlists. For larger deployments, firewall rules or a WAF are usually easier to manage and more efficient.
Conclusion
IP whitelisting with .htaccess is a simple way to restrict access to a directory or application. It is useful for internal tools, protected APIs, development environments, and other resources that should only be reachable from trusted networks.