Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

How to Whitelist IP Address Ranges with .htaccess

1 min read .
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 /admin or /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>
  • /24 covers addresses from 203.0.113.0 through 203.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

  1. Apache 2.4+ → The Require syntax applies to Apache 2.4 and later. Apache 2.2 used Allow from and Deny from.
  2. Test first → Validate access rules in a staging environment whenever possible. A bad rule can lock you out of the application.
  3. Scale appropriately.htaccess works 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.

Related Posts

chevron-up