.htaccess: how to restrict access to a single file by IP?

前端 未结 4 779
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 07:55

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,A         


        
相关标签:
4条回答
  • 2020-12-01 08:47

    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.

    0 讨论(0)
  • 2020-12-01 08:48

    I think the directive needs to be:

    Order deny,allow
    

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

    0 讨论(0)
  • 2020-12-01 08:50

    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.

    0 讨论(0)
  • 2020-12-01 08:51

    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

    0 讨论(0)
提交回复
热议问题