.htaccess:如何通过IP限制对单个文件的访问?

发布于 2024-09-16 10:42:44 字数 165 浏览 4 评论 0原文

我已经查看了所有内容,但不断遇到有关目录级 IP 限制的相同信息,通常看起来像这样:

Order Deny,Allow
Deny from all
Allow from 123.123.123.123

是否可以将相同类型的访问限制绑定到页面/文档?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

非要怀念 2024-09-23 10:42:44

这将允许来自 IP 127.0.0.1 的人作为有效用户登录。将其粘贴在您的配置或 .htaccess 文件中。

    <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:

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

这绝对回答了您的问题。

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.

无悔心 2024-09-23 10:42:44

我认为该指令需要是:

Order deny,allow

为了使上述答案起作用(至少对于单独的 IP 解决方案而言)。

I think the directive needs to be:

Order deny,allow

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

時窥 2024-09-23 10:42:44

基于 Mod-rewrite 的解决方案:

RewriteEngine on

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

如果客户端 ip 与 RewriteCond 模式中的 ip 地址不匹配,上面的 rewriteRule 将拒绝所有对 file.php 的请求

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

相权↑美人 2024-09-23 10:42:44

有关更新的 Apache 2.4 示例:

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

以下是有关其他选项和示例的更深入的文档:https://httpd.apache.org/docs/2.4/howto/access.html 以及 指令的文档:https://httpd.apache.org/docs/2.4/mod/core.html#文件

请注意, 可以嵌套在 部分中,以限制它们适用的文件系统部分。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文