Trigger apache authentication depending on URL parameter

412 Views Asked by At

I need to trigger the apache authentications depending only on a URL parameter. For instance, The following URL http://mySillyApplication.com/items/browse?collection=9&sort_field=Title&num_items=10&... I need to trigger the authentication only if collection=9 . I've tried many things but I couldn't find how to do it. I think the key is to find a RewriteRule that could activate the LocationMatch .... just a guess:

    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>

Thanks.

1

There are 1 best solutions below

1
Mohammed Elhag On

You could use <If> Directive if Apache is 2.4.26 and later http://httpd.apache.org/docs/2.4/mod/core.html#if

So, try the following code :

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