根据URL参数触发apache身份验证

时间:2018-03-15 10:53:03

标签: apache mod-rewrite locationmatch

我需要仅根据URL参数触发apache身份验证。例如,以下URL http://mySillyApplication.com/items/browse?collection=9&sort_field=Title&num_items=10& ...仅当collection = 9时才需要触发身份验证。我尝试了很多东西,但我找不到怎么做。我认为关键是找到一个可以激活LocationMatch的RewriteRule ....只是一个猜测:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} (.*(?:^|&))collection=9((?:&|$).*) [NC]
    RewriteRule (.*) - [R=401]
   <LocationMatch "....don't know...">
           AuthType Basic
           AuthName "Login Required"
           AuthUserFile /var/www/.../.htpwd
           Require valid-user
           Order allow,deny
           Allow from all
           Satisfy any
   </LocationMatch>

感谢。

1 个答案:

答案 0 :(得分:0)

如果Apache是​​2.4.26及更高版本http://httpd.apache.org/docs/2.4/mod/core.html#if

,您可以使用<If> Directive

因此,请尝试以下代码:

<If "%{QUERY_STRING} =~ /(collection=9&)/">

       AuthType Basic
       AuthName "Login Required"
       AuthUserFile /path/to/.htpasswd
       Require valid-user
       Order allow,deny
       Deny from all
       Satisfy any
</If>