1

Following situation:

  • I wish to redirect all IP adresses (but NOT two fixed ones) if accessing the www.mydomain.tld/SubFolder1/ on my apache to www.mydomain.tld
  • Offen basic authentication for the www.mydomain.tld/SubFolder1/ with differen usernames

any idea how to do that?

I tried to use one htaccess file where i added some redirection rules and the basic auth stuff. But I never got the redirection rules to work correctly. Seamed the auth stuff is overwriting the redirection rules. Could that be?

I use the following code for the Authentication

AuthName "Restricted"
AuthType Basic
AuthUserFile //is/htdocs/www/subfolder1/.htpasswd
AuthGroupFile /dev/null
require valid-user
4
  • Show your present code for using BASIC auth. Commented Jan 19, 2014 at 14:12
  • added the codepart for the Basic out I use. Commented Jan 21, 2014 at 17:11
  • Should basic authentication be shown to all the users before redirection? (including those 2 IPs)? Commented Jan 21, 2014 at 18:13
  • No, only the two IPs should see the basic authentication, all other users should be forwarded to the root domain without seeing the authentication. Commented Jan 22, 2014 at 18:40

1 Answer 1

2

You can use this code in your DOCUMENT_ROOT/SubFolder1/.htaccess file:

RewriteEngine On

RewriteCond %{REMOTE_ADDR} !^(192\.168\.0\.10|192\.168\.0\.20)$
RewriteRule ^$ http://www.mydomain.tld/ [L,R]

SetEnvIf Remote_Addr ^(192\.168\.0\.10|192\.168\.0\.20)$ DOAUTH

AuthName "Restricted"
AuthType Basic
AuthUserFile //is/htdocs/www/subfolder1/.htpasswd
AuthGroupFile /dev/null
require valid-user
Satisfy     any
Order       allow,deny
Allow from  all
Deny from env=DOAUTH

SetEnvIf is needed because mod_auth runs before mod_rewrite hence env set by mod_rewrite cannot be used mod_auth.

Sign up to request clarification or add additional context in comments.

2 Comments

Great, thanks ... By the way, is it possible to use more then one SetEnvIf line, for example if I wish to add 2 additional IPs? this would be handy if I would need to add my home dynamic dial up IP range here. With the pipe I might end up in a very long string
Oh yes sure you can have as many SetEnvIf lines as you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.