简体   繁体   English

结合基本身份验证并在.htaccess中重定向?

[英]Combine basic auth and redirect in .htaccess?

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 如果要访问我的apache上的www.mydomain.tld / SubFolder1 /,我希望将所有IP地址(而不是两个固定地址)重定向到www.mydomain.tld
  • Offen basic authentication for the www.mydomain.tld/SubFolder1/ with differen usernames 使用不同的用户名为www.mydomain.tld / SubFolder1 /提供基本身份验证

any idea how to do that? 知道怎么做吗?

I tried to use one htaccess file where i added some redirection rules and the basic auth stuff. 我尝试使用一个htaccess文件,在其中添加了一些重定向规则和基本身份验证内容。 But I never got the redirection rules to work correctly. 但是我从来没有重定向规则正常工作。 Seamed the auth stuff is overwriting the redirection rules. Seamed auth内容正在覆盖重定向规则。 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

You can use this code in your DOCUMENT_ROOT/SubFolder1/.htaccess file: 您可以在DOCUMENT_ROOT/SubFolder1/.htaccess文件中使用以下代码:

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 . SetEnvIf是需要的,因为mod_auth之前运行mod_rewrite因此ENV通过设置mod_rewrite不能使用mod_auth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM