I need to configure my Apache (2.4) in a way, that It prevents answering requests that

  1. don't come from the same IP as the server

  2. match a specific pattern in the URL

In pseudocode it must look something like this, but I'm having a hard time translating this in valid Apache config syntax

<If "%{CLIENT_IP} != %{SERVER_IP} &&  %{REQUEST_URL} contains 'abcde' ">
  Require all denied

1 Answer 1


You can create a list of allowed IPs and then compare the page or directory accessed against it.

SetEnvIfNoCase Remote_Addr allow_ip #AllowedServer1
SetEnvIfNoCase Remote_Addr allow_ip #AllowedServer2

<Directory /home/YOURSITE/public_html/secret/>
        require env allow_ip

Following the above code, access to the directory example.com/secret will only be allowed for IPs and Apache will deny access with a 403 error to all other visitors.

  • How does this answer the question. The OP is not asking about particular IP addresses, but rather a pattern. Commented Aug 5, 2022 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.