【发布时间】:2013-01-30 12:04:28
【问题描述】:
该网站位于共享主机上。我需要对单个 URL 进行密码保护。
http://www.example.com/pretty/url
显然这不是我要保护的物理文件路径,它只是那个特定的 URL。
.htaccess 有什么快速的解决方案吗?
【问题讨论】:
标签: .htaccess password-protection
该网站位于共享主机上。我需要对单个 URL 进行密码保护。
http://www.example.com/pretty/url
显然这不是我要保护的物理文件路径,它只是那个特定的 URL。
.htaccess 有什么快速的解决方案吗?
【问题讨论】:
标签: .htaccess password-protection
您应该能够使用 mod_env 和 Satisfy any 指令的组合来执行此操作。您可以使用SetEnvIf 来检查Request_URI,即使它不是物理路径。然后您可以检查变量是否在Allow 语句中设置。因此,要么您需要使用密码登录,要么Allow 让您无需密码即可登录:
# Do the regex check against the URI here, if match, set the "require_auth" var
SetEnvIf Request_URI ^/pretty/url require_auth=true
# Auth stuff
AuthUserFile /var/www/htpasswd
AuthName "Password Protected"
AuthType Basic
# Setup a deny/allow
Order Deny,Allow
# Deny from everyone
Deny from all
# except if either of these are satisfied
Satisfy any
# 1. a valid authenticated user
Require valid-user
# or 2. the "require_auth" var is NOT set
Allow from env=!require_auth
【讨论】:
Require valid-user 更改为Require user [your username]。您不能在 .htaccess 文件中定义密码,因为它不安全。您必须使用htpasswd 在您的服务器上创建一个 .htpasswd 文件,其中包含用户及其密码。
您可以在<VirtualHost> 指令中使用<LocationMatch> 或简单地使用<Location> 来执行此操作(假设您可以访问您的httpd.conf / vhost.conf - 或者您可以在您的.htaccess 中放置类似的内容如果您必须以这种方式配置您的网站,请记录根目录)。
例如:
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/blabla
# Other usual vhost configuration here
<Location /pretty/url>
AuthUserFile /path/to/.htpasswd
AuthGroupFile /dev/null
AuthName "Password Protected"
AuthType Basic
require valid-user
</Location>
</VirtualHost>
如果您想将正则表达式与漂亮的 URL 进行匹配,您可能会发现 <LocationMatch> 更有用。文档是here。
【讨论】:
由于 Rick 在评论中表示此问题的答案无效,因此这是我使用的 sn-p:
AuthName "Protected Area"
AuthType Basic
AuthUserFile /path/to/your/.htpasswd
AuthGroupFile /dev/null
SetEnvIf Request_URI .* noauth
SetEnvIf Request_URI the_uri_you_want_to_protect !noauth
SetEnvIf Request_URI another_uri !noauth
SetEnvIf Request_URI add_as_many_as_you_want !noauth
<RequireAny>
Require env noauth
Require valid-user
</RequireAny>
如果您需要支持 Apache 2.2 和 Apache 2.4(显然有两个版本并行运行的设置...):
AuthName "Protected Area"
AuthType Basic
AuthUserFile /path/to/your/.htpasswd
AuthGroupFile /dev/null
SetEnvIf Request_URI .* noauth
SetEnvIf Request_URI the_uri_you_want_to_protect !noauth
SetEnvIf Request_URI another_uri !noauth
SetEnvIf Request_URI add_as_many_as_you_want !noauth
<IfModule mod_authz_core.c>
<RequireAny>
Require env noauth
Require valid-user
</RequireAny>
</IfModule>
<IfModule !mod_authz_core.c>
Order Deny,Allow
Deny from all
Satisfy any
Require valid-user
Allow from env=noauth
</IfModule>
Apache 2.2 的代码取自Jon Lin。
【讨论】:
所有提供的解决方案都不适合我。我认为以下指令可以解决问题:
SetEnvIf Request_URI ^/page-url auth=1
AuthName "Please login"
AuthType Basic
AuthUserFile "/www/live.example.com/files/html/.htpasswd"
# first, allow everybody
Order Allow,Deny
Satisfy any
Allow from all
Require valid-user
# then, deny only if required
Deny from env=auth
【讨论】:
我更新了Jon Lin 的代码,以保护除一个之外的所有 URL - 例如虚拟主机根目录下的 robots.txt。这些是 Apache 2.2 兼容指令。
<ifmodule mod_setenvif.c>
SetEnv require_no_auth=false
SetEnvIf Request_URI "^/robots.txt" require_no_auth=true
AuthType Basic
AuthName "Restricted"
AuthUserFile /home/someuser/.htpasswd
# Setup a deny/allow
Order Deny,Allow
# Deny from everyone
Deny from all
# 1. a valid authenticated user
Require valid-user
# or 2. the "require_no_auth" var is set
Allow from env=require_no_auth
# except if either of these are satisfied
Satisfy any
</ifmodule>
Apache 2.4 的代码相同
<ifmodule mod_setenvif.c>
SetEnv require_no_auth=false
SetEnvIf Request_URI "^/robots.txt" require_no_auth=true
AuthType Basic
AuthBasicProvider file
AuthName "Restricted"
AuthUserFile /home/someuser/.htpasswd
# grant access if either of these are satisfied
# 1. a valid authenticated user
Require valid-user
# or 2. the "require_no_auth" var is set
Require env require_no_auth
</ifmodule>
【讨论】:
在 Apache2.4 中你可以这样做:
<If "%{REQUEST_URI} =~ m#/pretty/url/?#i">
AuthUserFile /var/www/htpasswd
AuthName "Password protected"
AuthType Basic
Require valid-user
</If>
【讨论】:
如何将用户重定向到受密码保护的子文件夹?
.htaccess
RewriteCond %{HTTP_COOKIE} !BadHorsie=secret_cookie_key
RewriteRule ^(pretty/url)$ /protected/login.php?url=$1 [R=307,L]
受保护的/.htaccess
AuthUserFile /usr/www/{YOUR_PATH}/protected/.htpasswd
AuthGroupFile /dev/null
AuthName "Protected"
AuthType Basic
require user BadHorsie
受保护的/.htpasswd
BadHorsie:$apr1$fFbaaVdF$Q5ql58g7R4qlpMUDb/5A0/
受保护的/login.php
<?php
if (isset($_GET['url']) && $_GET['url'] && $_GET['url'][0] != '/' && strpos($_GET['url'], '//') === false) {
setcookie('BadHorsie', 'secret_cookie_key', 0, '/');
header('Location: /' . $_GET['url'], true, 307);
exit;
}
?>
会发生什么
example.com/pretty/url
example.com/protected/login.php?url=pretty/url
example.com/pretty/url
注意:当然,“会话 cookie 和反向重定向”机制是完全可选的。最后,您可以直接通过protected/login.php 提供您的秘密内容。我展示这种方式只是为了获得灵感。
可选:不要使用 PHP 和 set the cookie through .htaccess.
【讨论】:
这个命令:
htpasswd -c /etc/apache2/.htpasswd myuser1
如果您使用的是Bitnami Apps(例如Open edx),您需要遵循以下说明:
htpasswd -c /opt/bitnami/apps/edx/conf/.htpasswd myuser1
这个配置:
<VirtualHost *:80>
...
<Location />
Deny from all
#Allow from (Set IP to allow access without password)
AuthUserFile /etc/apache2/.htpasswd
AuthName "Restricted Area"
AuthType Basic
Satisfy Any
require valid-user
</Location>
...
</VirtualHost>
此配置用于保护所有 URL。:
<Location />
Deny from all
#Allow from (Set IP to allow access without password)
AuthUserFile /opt/bitnami/apps/edx/conf/.htpasswd
AuthName "Restricted Area"
AuthType Basic
Satisfy Any
require valid-user
</Location>
在 Bitnami 应用程序中:
/opt/bitnami/ctlscript.sh restart apache
【讨论】:
This config to protect all URLs. - 问题是关于保护单个 URL
很遗憾,我无法评论 @jon-lin 的回答。因此,我创建了一个新答案。我在 apache 2.4 环境中的适应解决方案是:
#
# password protect /pretty/url URIs
#
AuthType Basic
AuthName 'Authentication required'
AuthUserFile /path/to/.htpasswd
# Restrict access to some urls
SetEnvIf Request_URI ^/pretty/url auth=1
<RequireAll>
# require the auth variable to be set
Require env auth
# require an valid-user
Require valid-user
</RequireAll>
【讨论】:
/pretty/url之外的任何其他url,因为如果没有设置变量,Require env auth将禁止访问
上面的解决方案太忙了,有点混乱。我喜欢这样简单明了
<Files "protected.html">
AuthName "Username and password required"
AuthUserFile /home/fullpath/.htpasswd
Require valid-user
AuthType Basic
</Files>
以上内容将被放入您的 htaccess 文件中,其中“protected.html”是您要保护的文件(可以是您想要的任何文件,例如 myphoto.jpg 或 document.zip,只需重命名根据您的喜好)。 AuthUserFile 是密码文件的完整路径。你完成了。
尽管您可能必须确保已设置此先决条件。 Apache 目录标签配置中需要 AllowOverride 和 AuthConfig 才能使代码正常工作,但通常它是开箱即用的,因此除非您进行自己的自定义构建,否则您应该安全无虞。
【讨论】: