Home > front end >  Apache: If url starts with string, grant access otherwise use IP whitelisting
Apache: If url starts with string, grant access otherwise use IP whitelisting

Time:07-13

In Apache 2.4, i'm trying to use IP whitelisting on my entire site, expect some specific URL patterns:

  • /wp-json/hl/v1/*

  • /hl/images/*

Note: I've currently disabled my own IP for testing. When I try to access any URL I get a 403, but really I should only get a 403 outside aforementioned URL patterns.

Currently my VirtualHost looks like this:

<VirtualHost *:443>
    ...other configs

    <If "%{REQUEST_URI} =~ m#^/wp-json/hl/v1/.*$#">
            Require all granted
    </If>

    <ElseIf "%{REQUEST_URI} =~ m#^/hl/images/.*$#">
            Require all granted
    </ElseIf>

    <Else>
        <RequireAny>
            Require ip ::1
            Require ip 127.0.0.1
            Require ip [MY_IP]
        </RequireAny>
    </Else>
</VirtualHost>

Any ideas are welcome! Thank you in advance.

CodePudding user response:

Try THE_REQUEST instead of REQUEST_URI and combine Elseif into If using regex alternation:

    <If "%{THE_REQUEST} =~ m#^/(wp-json/hl/v1|hl/images)/#">
            Require all granted
    </If>
    <Else>
        <RequireAny>
            Require ip ::1
            Require ip 127.0.0.1
            Require ip [MY_IP]
        </RequireAny>
    </Else>
  • Related