微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

仅在访问特定域时才使用HTTP身份validation

我有几个网站: example.com , example1.com和example2.com 。 所有这些指向我的服务器的/public_html文件夹,这是我的Apache根文件夹。

只有当用户来自example2.com我需要添加到我的.htaccess文件才能使用httpauthentication? example.com和example1.com不应该使用身份validation。

我知道我需要类似的东西

AuthType Basic AuthName "Password required" AuthUserFile "/path/to/.htpasswd" Require valid-user

但是,如果用户访问example2.com我只想要一个密码

Mod_rewrite将example.com/page.PHP?v1=abc&v2=def改为example.com/abc/def

Apache RewriteMap用于防止直接访问文件

如何删除* .PHP,index.PHP和从ulrs,#ancors和?htaccess查询中的尾部斜杠

htaccess mod_rewrite

RewriteBase做什么以及如何使用它?

编辑

使用在答案中build议的方法,我的.htaccess文件中有以下内容

SetEnvIfNoCase Host ^(.*)$ testauth <IfDefine testauth> RewriteRule ^(.*)$ index2.PHP?q=$1 [L,QSA] </IfDefine>

我知道mod_setenvif.c模块是启用的(我用<IfModule>块validation),但似乎“testauth”永远不会被定义,因为我的testingvalidation(redirect到index2.PHP)没有执行(而它正在执行我的<IfModule>块)。 任何想法为什么?

Apache mod_rewrite – 优先考虑具有相当URL的目录上的文件

htaccess文件以某种方式被caching?

当目录存在时,htaccessredirect无法redirect

.htaccess 301将所有httpsredirect到http EXCEPT ONE PAGE

符号链接和索引文件无法正常工作的.htaccessconfiguration

在文档根目录的htaccess文件中,如何处理这些内容

# set the "require_auth" var if Host ends with "example2.com" SetEnvIfNoCase Host example2.com$ require_auth=true # Auth stuff AuthUserFile /var/www/htpasswd AuthName "Password Protected" AuthType Basic # Setup a deny/allow Order Deny,Allow # Allow for everyone Allow from all # Except if a auth is required Deny from env=require_auth # But if you have a user you are welcome Require valid-user # Auth OR access,not AND https://wiki.apache.org/httpd/BypassAuthenticationorAuthorizationRequirements Satisfy any

这将使得它不需要身份验证,除非主机以example2.com结尾(例如www.example2.com , dev.example2.com等)。 表达式可以调整,如果需要的话。 任何其他主机将导致require_auth var不被设置,因此不需要身份验证。 如果需要Allow from env=require_auth ,最后一行可以改为: Allow from env=require_auth ,删除

以下是一个建议:

创建一个名为common.conf的文件并保存在一个可访问的位置

在这文件中,将所有站点(主机)共同使用的Apache配置。

删除当前的单个VirtualHost条目,替换为VirtualHost条目,如下所示:

# These are the password protected hosts <VirtualHost *:80> serverName example.com serverAlias example1.com Include /path-to-common-configuration/common.conf AuthType Basic AuthName "Password required" AuthUserFile "/path/to/.htpasswd" Require valid-user </VirtualHost> # These are hosts not requiring authentication <VirtualHost *:80> serverName example2.com serverAlias example3.com Include /path-to-common-configuration/common.conf </VirtualHost>

您不应该将每个虚拟主机配置放入.htaccess。 相反,将配置块放在/etc/apache/sites-enabled/*的适当配置文件中的VirtualHost块中。

我想知道是否可以通过允许访问每个IP地址的方法来帮助DanH?

就像是

SetEnvIf Remote_Addr 1.2.3.4 AllowMeIn SetEnvIfNoCase Host this.host.is.ok.com AllowMeIn SetEnvIfNoCase Host this.host.is.also.ok.com AllowMeIn

然后在你的Drupal“容器”

Order Allow,Deny Allow from env=AllowMeIn

应该做的伎俩。

任何“活”的主机都应该配置为“AllowMeIn”,否则你必须来自一个已知的IP地址(即你和其他开发者)。

Apache 2.4提供了一个If语句的语义选择:

<If "req('Host') == 'example2.com'"> AuthUserFile /path/to/htpasswd AuthType Basic AuthName "Password Protected" Require valid-user </If> <Else> Require all granted </Else>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐