获取 .htaccess 以允许访问指定的 IP,但如果来自任何其他 IP,则请求 .htpasswd 用户 + 密码

Getting .htaccess to allow access for specified IP, but request .htpasswd user + password if from any other IP

我有一个站点正在尝试设置 .htaccess / .htpasswd 密码限制,但同时允许任何具有特定 IP 地址的用户访问。

到目前为止,我已经在我的 .htaccess 文件中实现了这一点,但出于某种原因,如果我访问该站点,无论它允许从哪个 IP 地址访问,如果我注释掉行 order deny,allowAllow from 192.87.22.18 行上方,密码保护有效,但会向每个人请求一个 IP,即使从 192.87.22.18 IP 访问(注意 - 不是我的真实 IP)

(出于显而易见的原因,我没有发布我的 .htpasswd 文件,但它是包含 1 个散列密码的 1 行)

有什么想法吗?

<Files ~ "^\.(htaccess|.htpasswd)$">
    deny from all
    </Files>

    DirectoryIndex index.php
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.example\.example\.co\.uk)(:80)? [NC]
    RewriteRule ^(.*) http://example.example.co.uk/ [R=301,L]
    order deny,allow

    <Files ~ "^\.(htaccess|.htpasswd)$">
    deny from all
    </Files>
AuthType Basic
AuthName "Please enter your ID and password"
    AuthUserFile /var/www/vhosts/example.co.uk/subdomains/example/httpdocs/.htpasswd
    AuthGroupFile /dev/null
    require valid-user 
    order deny,allow
    Allow from 192.87.22.18
    # satisfy any

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php(/(.*)|$) %{CONTEXT_PREFIX}/ [R=301,L]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]
    RewriteCond %{REQUEST_URI}:: ^(/.+)(.+)::$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteRule .? %{ENV:BASE}index.php [L]

4 你应该使用:

AuthType Basic
AuthUserFile c:/wamp/www/_test/www/.htpasswd
AuthName "Protected Area"

<RequireAny>
    Require ip 127.0.0.1
    Require valid-user
</RequireAny>

orderdenyallow 现已弃用。

通过反复试验,我最终使用了以下适用于 apache 2.0 的代码,取自此处:https://perishablepress.com/htaccess-password-protection-tricks/

在上面的文章中,他们将代码块包装在 <IfModule mod_auth.c> 中,这导致代码不 运行 (因为我猜 if 块在这个服务器上等同于 false)所以我删除了它们.

    # password protect excluding specific ip
 AuthName "Username and password required"
 AuthUserFile /home/path/.htpasswd
 AuthType Basic
 Require valid-user
 Order Deny,Allow
 Deny from all
 Allow from 111.222.333.444
 Satisfy Any