24

I've look all over, but keeps running into same info that talks about directory level IP restriction, which usually looks something like this:

Order Deny,Allow
Deny from all
Allow from 123.123.123.123

Is it possible to have same type of access restriction tied to a page/document?

4 Answers 4

37

This will allow either someone from IP 127.0.0.1 or logged as a valid user. Stick it either in your config or .htaccess file.

    <Files learn.php>
        Satisfy any
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1

        AuthType Basic
        AuthName "private"
        AuthUserFile /var/www/phpexperts.pro/.htpasswd
        AuthGroupFile /dev/null
        Require valid-user
    </Files>

IP Alone:

    <Files learn.php>
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
    </Files>

That definitely answers your question.

4
  • Fastest answer response ever! Commented Aug 30, 2010 at 22:14
  • @TheodoreR.Smith : if I want to deny from one IP but allow from all others then what to write in htaccess?
    – sqlchild
    Commented Feb 6, 2014 at 15:41
  • Change to Allow from all Deny from IP_ADDRESS. Commented Feb 7, 2014 at 18:20
  • @TheodoreR.Smith, I tried your code but I am getting Access forbidden! from my ip address. I have to access the service.php page from my single ip. <Files service.php> Order deny, allow Deny from all Allow from 0.0.0.0 </Files> prnt.sc/vtkknw Commented Dec 1, 2020 at 12:41
9

I think the directive needs to be:

Order deny,allow

for the answer above to work (at least for the IP Alone solution).

1
  • How to block an organization to open my website from their PCs ? They have 10 PCs connected on LAN and they need to access my website from only 5 of their PCs and block on the other 5? How do I achieve this?
    – sqlchild
    Commented Feb 6, 2014 at 18:12
1

Mod-rewrite based solution :

RewriteEngine on

RewriteCond %{REMOTE_ADDR} !^Y\.O\.U\.R\.IP$
RewriteRule ^file\.php$ - [F,L]

The rewriteRule above will deny all requests to file.php if client ip does not match the ip address in the RewriteCond's pattern

2
  • This answer is not clear to implement for many developers.
    – landed
    Commented Aug 9, 2021 at 10:34
  • @landed Could you please explain what's not clear in my answer? This is a mod-rewrite solution to protect a file from an ip address and it's the easiest , fastest,and safest way to do so.
    – Amit Verma
    Commented Aug 9, 2021 at 12:06
1

For a more up to date Apache 2.4 example:

<Files file.html>
    Require ip 123.123.123.123
</Files>

Here are the more in depth docs for additional options and examples: https://httpd.apache.org/docs/2.4/howto/access.html and the docs for the <Files> directive: https://httpd.apache.org/docs/2.4/mod/core.html#files

Note that <Files> can be nested inside <Directory> sections to restrict the portion of the filesystem they apply to.

1
  • is this still current?
    – oldboy
    Commented Mar 16 at 5:24

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.