Search code examples
.htaccess

How to restrict access to a url/sub-url based on ip?


I would like https://example.com/wp-json/ and everything underneath that (so /wp-json/xxx as well as /wp-json/xxx/xxx) to be accessible from 2 IP addresses only using the htaccess file.

So at the top of my htaccess file I added:

<RequireAll>
    Require all granted
    Require ip 111.111.111.111 22.222.222.222
</RequireAll>

But how do I add the condition in htaccess that this only applies to https://example.com/wp-json/ and its subfolders? Something like:

<If url includes https://example.com/wp-json/>
    <RequireAll>
        Require all granted
        Require ip 111.111.111.111 22.222.222.222
    </RequireAll>
</If>

Solution

  • <If url includes https://example.com/wp-json/>
    
    <If "%{REQUEST_URI} =~ m#^/wp-json/#">
    

    Reference: