Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Apache: If url starts with string, grant access otherwise use IP whitelisting

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/*

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

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.

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading