Amauche
Amauche

Reputation: 35

RewriteRule / HTTP auth for one url

I want to redirect "?p_id=62" to http authentification but it does not work.

my conf:

<LocationMatch  ^/>
   RewriteCond %{QUERY_STRING} !^\?p_id=62$
   RewriteRule ^ - [E=NO_AUTH:1,L]
   Order deny,allow
   Deny from all
   AuthType basic
   AuthName "Auth Needed"
   AuthUserFile "/var/www/site/.htpasswd"
   Require valid-user
   Allow from env=NO_AUTH
   Satisfy Any
</LocationMatch>

Can anyone help?

Upvotes: 1

Views: 869

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

You don't need to match the ? part of the query string:

RewriteCond %{QUERY_STRING} !^p_id=62$
RewriteRule ^ - [E=NO_AUTH:1,L]

However, this is not going to work. The mod_auth modules are applied before mod_rewrite gets applied, so by the time the rewrite rule checks the query string to set the environment variable, mod_auth has already flagged the request as a 401 (needs auth). You might have to settle for a scripted solution, like:

RewriteCond %{QUERY_STRING} !^\?p_id=62$
RewriteRule ^ /auth_script.php [L]

<Files "auth_script.php">
 Order deny,allow
 Deny from all
 AuthType basic
 AuthName "Auth Needed"
 AuthUserFile "/var/www/site/.htpasswd"
 Require valid-user
</Files>

and the auth_script.php will simply load the content at the $_SERVER['REQUEST_URI'] URI and return it to the browser (not redirect).

Upvotes: 1

Related Questions