【问题标题】:Getting .htaccess to allow access for specified IP, but request .htpasswd user + password if from any other IP获取 .htaccess 以允许访问指定 IP,但如果来自任何其他 IP,则请求 .htpasswd 用户 + 密码
【发布时间】:2016-07-26 12:53:37
【问题描述】:

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

到目前为止,我在我的 .htaccess 文件中实现了这个,但由于某种原因,如果我访问该站点,无论它允许从哪个 IP 地址访问,如果我注释掉上面的行 order deny,allow,这将不起作用Allow 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/$1 [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}/$2 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteRule .? %{ENV:BASE}index.php [L]

【问题讨论】:

  • 你在htaccess中还有其他指令吗?
  • @starkeen 是的,但它只与 php 配置有关,所以我没有将它包含在上面,这就是它:RemoveType .php AddHandler application/php5-fcgi php Action application/php5-fcgi /cgi-bin/php5fcgi.fcgi

标签: apache .htaccess authentication .htpasswd


【解决方案1】:

通过反复试验,我最终使用了适用于 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

【讨论】:

    【解决方案2】:

    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 现已弃用。

    【讨论】:

    • 谢谢,但这最终会导致服务器错误,您知道运行上述所需的 LAMP 最低规格吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2012-01-04
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多