I've produced a question based authentication on my small apache server, but I have got an unusual behavior from apache, and should not find my way using this. This is actually the .htaccess code:

RewriteEngine on
RewriteCond %{QUERY_STRING} p=1
RewriteRule ^(.*)$ $1?map=$1 [E=authme:1]
Order deny,allow
Deny from env=authme
AuthType Basic 
AuthName "Login Required"
AuthUserFile /var/www/test/.htpasswd
require valid_user
Satisfy any

I expect apache to check out query string for p=1. If found an environment variable is placed (authme=1). This works fine!
I expect then that authentication is needed (Deny from env=authme) if p=1 exists in query string.
All of this things works wonderfully if your call in the browser the primary the index file (during my situation index.php), but doesn't work on all if your call some other file within the same directory.
Environment variable is properly set (I'm able to check it with PHP), but no authentication is needed.

I truly can't realise why. Any assistance will be appreciated!

More information are available here

Have you got every other rewrite rules after that one? Perhaps you have attempted which makes it a final [L] rule? Also provide you attempted inside a second browser which has never authenticated on any web addresses?

require valid_user

ought to be

require valid-user