htaccess从Basic Auth中排除一个URL

Krz*_*Dan 72 apache .htaccess authorization

我需要从正常的htaccess Basic Auth保护中排除一个Url(甚至更好的一个前缀).像/ callbacks/myBank/ callbacks /之类的东西.*你有任何提示怎么做?

我不想要的是如何排除文件.这必须是url(因为这是基于PHP框架的解决方案,并且所有url都使用mod_rewrite重定向到index.php).所以这个网址下没有文件.没有.

其中一些网址只是来自其他服务的回调(没有IP是未知的,所以我不能基于IP排除),他们无法提示用户/密码.

目前的定义很简单:

AuthName "Please login."
AuthGroupFile /dev/null
AuthType Basic
AuthUserFile /xxx/.htpasswd
require valid-user 
Run Code Online (Sandbox Code Playgroud)

Jon*_*Lin 82

使用SetEnvIf,您可以在请求以某个路径开始时创建变量,然后使用该Satisfy Any指令以避免必须登录.

# set an environtment variable "noauth" if the request starts with "/callbacks/"
SetEnvIf Request_URI ^/callbacks/ noauth=1

# the auth block
AuthName "Please login."
AuthGroupFile /dev/null
AuthType Basic
AuthUserFile /xxx/.htpasswd

# Here is where we allow/deny
Order Deny,Allow
Satisfy any
Deny from all
Require valid-user
Allow from env=noauth
Run Code Online (Sandbox Code Playgroud)

allow/deny指令块表示拒绝访问EVERYONE,除非有有效用户(成功的BASIC auth登录)或noauth设置了变量.

  • 我需要*两行`Allow from env=noauth` 和`Allow from env=REDIRECT_noauth` 才能工作。 (3认同)
  • 可能需要“允许来自env = REDIRECT_noauth”来重写网址。@vishal可能是问题所在。在这里看看:[http://stackoverflow.com/a/41092497/1285585] (2认同)

bep*_*ter 49

如果您使用的是Apache 2.4,则不再需要SetEnvIf和mod_rewrite解决方法,因为该SetEnvIf指令能够直接解释表达式:

AuthType Basic
AuthName "Please login."
AuthUserFile "/xxx/.htpasswd"

Require expr %{REQUEST_URI} =~ m#^/callbacks/.*#
Require valid-user
Run Code Online (Sandbox Code Playgroud)

Apache 2.4将未按其分组的Require指令视为a ,其行为为"或"语句.这是一个更复杂的示例,演示了将请求URI和查询字符串一起匹配,并回退到需要有效用户:Require<RequireAll>

AuthType Basic
AuthName "Please login."
AuthUserFile "/xxx/.htpasswd"

<RequireAny>
    <RequireAll>
        # I'm using the alternate matching form here so I don't have
        # to escape the /'s in the URL.
        Require expr %{REQUEST_URI} =~ m#^/callbacks/.*#

        # You can also match on the query string, which is more
        # convenient than SetEnvIf.
        #Require expr %{QUERY_STRING} = 'secret_var=42'
    </RequireAll>

    Require valid-user
</RequireAny>
Run Code Online (Sandbox Code Playgroud)

此示例允许访问<RequireAny>但需要用户名和密码/callbacks/foo?secret_var=42.

请记住,除非您使用/callbacks/foo,否则Apache将尝试<RequireAll> 按顺序匹配每个,因此请考虑您首先要允许的条件.

Require指令的参考资料如下:https://httpd.apache.org/docs/2.4/mod/mod_authz_core.html#require

Require分裂国家基准是在这里:https://httpd.apache.org/docs/2.4/expr.html

  • 我不得不删除Require expr周围的双引号,以便它可以在apache 2.4.7上运行.除此之外完美无缺. (3认同)
  • 我怀疑@ stephan-richter因为'%{REQUEST_URI}`值是重写的字符串.对于Cake 3,它最终总是为`/ webroot/index.php`.您可能必须提出另一种方法,例如使用Cake本身来处理路由的身份验证. (3认同)

小智 6

此解决方案非常有效,您只需要定义要通过的白名单.

SetEnvIfNoCase Request_URI "^/status\.php" noauth

AuthType Basic
AuthName "Identify yourself"
AuthUserFile /path/to/.htpasswd
Require valid-user

Order Deny,Allow
Deny from all
Allow from env=noauth

Satisfy any
Run Code Online (Sandbox Code Playgroud)


Mag*_*Man 6

我尝试了其他解决方案,但这对我有用。希望这对其他人有帮助。

# Auth stuff
AuthName "Authorized personnel only."
AuthType Basic
AuthUserFile /path/to/your/htpasswd/file

SetEnvIf Request_URI "^/index.php/api/*" allow
Order allow,deny
Require valid-user
Allow from env=allow
Deny from env=!allow
Satisfy any
Run Code Online (Sandbox Code Playgroud)

这将允许 api url 和之后的任何 url 字符串/index.php/api/无需登录即可打开,并且将提示登录其他任何内容。

例子:

mywebsite.com/index.php/api将打开而不提示登录 mywebsite.com/index.php/api/soap/?wsdl=1将打开而不提示登录 mywebsite.com将首先提示登录